C#使用WebService

一、新建webservice

  1. 新建项目→asp.net Web服务应用程序
  2. 或者在现有项目中 点击右键 新建web服务程序asmx
  3. 只要在webservice类里面 的方法 标注为【WebMethod】就成为别人可以调的方法
  4.                       
  5. 如果要返回DataTable  
  6. 只要 DataTable.TableName 不为空, 即可返回
    否则不行.
  7. 二、webservice调用

引用之后直接就可以使用里面的方法了 命名空间 类名 client=new 类; client.方法()

常用错误

①无法加载协定为“ServiceReference1.InterfaceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的

如果出现以上错误是因为第一次引用webservice的时候已经在webconfig里面产生了<endpoint>配置节点,首次运行的时候又一次加了那么一个配置节点重复了,需要手动删除一个节点原因是在web.config 文件中多次引用了“添加外部引用”

 <client>
      <endpoint address="http://218.90.168.115:8000/PJSDFacade/PJSD/Interface.asmx"
        binding="basicHttpBinding" bindingConfiguration="InterfaceSoap"
        contract="ServiceReference1.InterfaceSoap" name="InterfaceSoap" />
<!-- 下面节点删除--> <endpoint address="http://218.90.168.115:8000/PJSDFacade/PJSD/Interface.asmx" binding="customBinding" bindingConfiguration="InterfaceSoap12" contract="ServiceReference1.InterfaceSoap" name="InterfaceSoap12" /> </client>
所以删掉一个节点既可(如查引用的是WebServiceSoap,删掉WebServiceSoap1的有关节点,反之~)

也可以在页面引用的时候指定bindingConfiguration名字:

如:ServiceReference.WebServiceSoap web = new WebServiceSoapClient("InterfaceSoap");

在调用webservice返回数据的时候, 出现以下错误:

已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性

这个就需要在调用webservice的解决方案中,在web.config或者app.config中配置一下:注意红色字体为哪个节点下加的哪些配置。

  <system.serviceModel>
        <bindings >
              <basicHttpBinding>
                    <binding name="InterfaceSoap" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"   />
              </basicHttpBinding>
              <customBinding>
                    <binding name="InterfaceSoap12"   >
                          <textMessageEncoding messageVersion="Soap12" />
                          <httpTransport />
                    </binding>
              </customBinding>
        </bindings>
        <client>
              <endpoint address="http://218.90.168.115:8000/PJSDFacade/PJSD/Interface.asmx"
                    binding="basicHttpBinding" bindingConfiguration="InterfaceSoap"
                    contract="ServiceReference.InterfaceSoap" name="InterfaceSoap" />
             
        </client>
    </system.serviceModel>

   如果在本地测试webservice可以运行,在远程却显示“测试窗体只能用于来自本地计算机的请求”或者"The test form is only available for requests from the local machine. ",那是因为没有开启远程访问的原因。
     大家都知道,Web服务做好后,发布在网上,别人要调用你提供的接口时,是无法打开测试窗体的,这让很多的朋友都蛮郁闷,为什么别人提供的服务接口就能够打开测试窗体,而我的就不行呢?是不是我的代码写的有问题呢?其实不是这样的,下面,我就来教你如何实现这个功能,让客户端也能够打开测试窗体。
1:在web.config的</system.web>中间加入如下配置节内容
<system.web>
<webServices>
         <protocols>
            <add name="HttpSoap"/>
            <add name="HttpPost"/>
            <add name="HttpGet"/>
            <add name="Documentation"/>
         </protocols>
</webServices>
</system.web>
2.通过编辑 Machine.config 中的 <protocols> 节为计算机上的所有 Web 服务启用这些协议。下面的示例启用了 HTTP GET、HTTP POST 及 SOAP,此外还从本地主机启用了 HTTP POST:
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="HttpPostLocalhost"/>
<!-- Documentation enables the documentation/test pages -->
<add name="Documentation"/>
</protocols>
http://stu-xu.i.sohu.com/blog/view/170429191.htm
http://blog.csdn.net/wangtao790108/article/details/5568281

原文地址:https://www.cnblogs.com/yabisi/p/6010604.html