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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM