編寫WsHttpBinding的WCF通信方式


這個通信方式本人實驗了好久,需要一個重要的條件是服務端和客戶端的發送內容方式都是相同的聲明,需要在配置文件寫入,客戶端:

 <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"  />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"  />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" />
            <message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>

客戶端代碼:

  try
            {
                var myBinding = new WSHttpBinding("wsHttpBinding");//根據后台的binding名字選中后台的binding
                var myEndpoint =
                    new EndpointAddress(
                        new Uri("http://localhost:12857/UserService.svc"));
                var myChannelFactory = new ChannelFactory<IUserBussiness>(myBinding, myEndpoint);
                IUserBussiness client = myChannelFactory.CreateChannel();
                var res = client.DoWork("1111");
                myChannelFactory.Close();
            }
            catch (Exception ex)
            {
                //do something proper here 
            }

服務端的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFTestImp.UserBussiness" behaviorConfiguration="metadataBehavior">
        <!--這里指定要綁定的binding,contract指定的是WCF的接口-->
        <endpoint bindingConfiguration="wsHttpBindings" address="" name="WCFTestImp.UserBussiness" binding="wsHttpBinding" contract="WCFTestImp.IUserBussiness" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
           <!--為避免泄漏元數據信息,請在部署前將以下值設置為 false--> 
          <serviceMetadata httpGetEnabled="true"/>
           <!--要接收故障異常詳細信息以進行調試,請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息--> 
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceSecurityAudit auditLogLocation="Default" suppressAuditFailure="True" serviceAuthorizationAuditLevel="SuccessOrFailure" messageAuthenticationAuditLevel="Success" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
    
    <bindings>
      <!--這里聲明服務端的binding也是wsHttpBinding的方式-->
      <wsHttpBinding>
        <binding name="wsHttpBindings" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" />
            <message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
   
  </system.serviceModel>
  <system.webServer>
    
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        若要在調試過程中瀏覽 Web 應用程序根目錄,請將下面的值設置為 True。
        在部署之前將該值設置為 False 可避免泄露 Web 應用程序文件夾信息。
      -->
    <directoryBrowse enabled="true"/>
    
  </system.webServer>
</configuration>

 不管是BasicHttpBinding還是WSHttpBinding又或者其他的綁定方式,服務端的接口一定要帶有標記寫法如下:

  [ServiceContract]
    public interface IUserBussiness
    {
        [OperationContract]
        string DoWork(string name);
    }

實現上面也要有標記,如下:

    [ServiceBehavior]
    public class UserBussiness:IUserBussiness
    {
        public string DoWork(string name)
        {
            return string.Format("hello Word by {0}", name);
        }
    }

 


免責聲明!

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



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