Web服务basicHTTPBinding

时间:2014-12-16 15:18:49

标签: c# web-services wcf-binding wshttpbinding basichttpbinding

我是Web服务的新手,我需要将basicHTTPBinding实现到使用WSHttpBinding的现有wpf c#桌面应用程序。 Web服务已经配置为两者 Web服务上的web.config文件中的绑定如下:

<bindings>
  <wsHttpBinding>
    <binding name="HI_SSLBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Certificate" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" negotiateServiceCredential="True" establishSecurityContext="True" />
      </security>
    </binding>
  </wsHttpBinding>

  <basicHttpBinding>
    <binding name="HI_BasicBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <security mode="Transport">
        <transport clientCredentialType="Certificate" proxyCredentialType="None" realm="" />
      </security>
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

<services>
  <service name="HIWebService.HIService" behaviorConfiguration="SecureBehave">
    <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="HI_BasicBinding" contract="HILib.HIService.IHIService"></endpoint>
    <endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="HI_SSLBinding" contract="HILib.HIService.IHIService"></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="https://10.50.1.85:1345/HI/HIService.svc" />
      </baseAddresses>
    </host>
  </service>
</services>

wpf c#桌面应用程序让App.configer只使用Web服务上的wsHttpBinding,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
       <bindings>
  <wsHttpBinding>
    <binding name="HI_SSLBinding" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
        allowCookies="false">

        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />

      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Certificate" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" negotiateServiceCredential="True" establishSecurityContext="True"/>
      </security>
     </binding>
  </wsHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior>
      <clientCredentials>
        <serviceCertificate>
          <authentication certificateValidationMode="PeerOrChainTrust"/>
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="https://10.50.1.85:1345/HI/HIService.svc" binding="wsHttpBinding" bindingConfiguration="HI_SSLBinding" contract="HILib.HIService.IHIService">

  </endpoint>
</client> 
</system.serviceModel>
 </configuration>

现在,当应用程序使用wsHttpBinding时,它会执行以下操作:

  System.ServiceModel.WSHttpBinding wsHTTPSBinding = new System.ServiceModel.WSHttpBinding();
            wsHTTPSBinding.Name = "HIBridge_SSLBinding";
            wsHTTPSBinding.OpenTimeout = TimeSpan.FromMinutes(1);
            wsHTTPSBinding.CloseTimeout = TimeSpan.FromMinutes(1);
            wsHTTPSBinding.SendTimeout = TimeSpan.FromMinutes(1);
            wsHTTPSBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            wsHTTPSBinding.BypassProxyOnLocal = false;
            wsHTTPSBinding.TransactionFlow = false;
            wsHTTPSBinding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
            wsHTTPSBinding.MaxBufferPoolSize = 2147483647;
            wsHTTPSBinding.MaxReceivedMessageSize = 2147483647;
            wsHTTPSBinding.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
            wsHTTPSBinding.TextEncoding = Encoding.UTF8;
            wsHTTPSBinding.UseDefaultWebProxy = true;
            wsHTTPSBinding.AllowCookies = false;


            wsHTTPSBinding.ReliableSession.Ordered = true;
            wsHTTPSBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
            wsHTTPSBinding.ReliableSession.Enabled = false;

            wsHTTPSBinding.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential;
            wsHTTPSBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Certificate;
            wsHTTPSBinding.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
            wsHTTPSBinding.Security.Transport.Realm = "";
            wsHTTPSBinding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName;
            wsHTTPSBinding.Security.Message.NegotiateServiceCredential = true;
            wsHTTPSBinding.Security.Message.EstablishSecurityContext = true;
            System.ServiceModel.EndpointAddress endpointAddress = null;

            endpointAddress = new System.ServiceModel.EndpointAddress(string.Format("https://10.50.1.85:1345/HI/HIService.svc/ws");

如何为BasicHttpBinding配置App.configer以及如何在c#wpf应用程序上调用此绑定?

1 个答案:

答案 0 :(得分:1)

你能试试吗?

的app.config

<configuration>
    <system.serviceModel>
      <bindings>
        <basicHttpBinding>
          <binding name="basicHttp_HI_SSLBinding" closeTimeout="00:01:00"
              openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
              bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
              maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
              messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
              allowCookies="false">

            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />                                    
            <security mode="TransportWithMessageCredential">
              <transport clientCredentialType="Certificate" proxyCredentialType="None" realm="" />
              <message clientCredentialType="UserName" />
            </security>
          </binding>
        </basicHttpBinding>
      </bindings>
      <behaviors>
        <endpointBehaviors>          
          <behavior name="endpointBehavivor">
            <clientCredentials>
              <serviceCertificate>
                <authentication certificateValidationMode="PeerOrChainTrust"/>
              </serviceCertificate>
            </clientCredentials>
          </behavior>
        </endpointBehaviors>
      </behaviors>
      <client>
        <endpoint address="https://10.50.1.85:1345/HI/HIService.svc" behaviorConfiguration="endpointBehavivor" binding="basicHttpBinding" bindingConfiguration="basicHttp_HI_SSLBinding" contract="HILib.HIService.IHIService">
        </endpoint>
      </client>
    </system.serviceModel>
  </configuration>

并消费它

 //define binding
            BasicHttpBinding myBinding = new BasicHttpBinding();
            myBinding.Name = "basicHttp_HI_SSLBinding";
            myBinding.OpenTimeout = TimeSpan.FromMinutes(1);
            myBinding.CloseTimeout = TimeSpan.FromMinutes(1);
            myBinding.SendTimeout = TimeSpan.FromMinutes(1);
            myBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            myBinding.BypassProxyOnLocal = false;
            myBinding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
            myBinding.MaxBufferPoolSize = 2147483647;
            myBinding.MaxReceivedMessageSize = 2147483647;
            myBinding.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
            myBinding.TextEncoding = Encoding.UTF8;
            myBinding.UseDefaultWebProxy = true;
            myBinding.AllowCookies = false;
            myBinding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
            myBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Certificate;
            myBinding.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
            myBinding.Security.Transport.Realm = "";

            //define endpoint url              
            EndpointAddress myEndpoint = new EndpointAddress("https://10.50.1.85:1345/HI/HIService.svc/ws");

            //Use channle factory to autogenerate proxy class 
            ChannelFactory<HILib.HIService.IHIService> myChannelFactory = new ChannelFactory<HILib.HIService.IHIService>(myBinding, myEndpoint);
            HILib.HIService.IHIService HIService = myChannelFactory.CreateChannel();

            //and call it
            var result = HIService.SomeMethodcall();

            ((IClientChannel)HIService).Close();
            myChannelFactory.Close();