C#使用WebService

1、新建webservice

  1. 新建項目→asp.net Web服務應用程序
  2. 或者在現有項目中 點擊右鍵 新建web服務程序asmx
  3. 只要在webservice類裏面 的方法 標註爲【WebMethod】就成爲別人能夠調的方法
  4.                       
  5. 若是要返回DataTable  
  6. 只要 DataTable.TableName 不爲空, 便可返回
    不然不行.
  7. 2、webservice調用

引用以後直接就可使用裏面的方法了 命名空間 類名 client=new 類; client.方法()web

 

經常使用錯誤app

①沒法加載協定爲「ServiceReference1.InterfaceSoap」的終結點配置部分,由於找到了該協定的多個終結點配置。請按名稱指示首選的asp.net

若是出現以上錯誤是由於第一次引用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 屬性spa

這個就須要在調用webservice的解決方案中,在web.config或者app.config中配置一下:注意紅色字體爲哪一個節點下加的哪些配置。.net

  <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/5568281code

相關文章
相關標籤/搜索