在WCF服务中支持Soap12

本文关键字:支持 Soap12 服务 WCF | 更新日期: 2023-09-27 17:49:51

我在WCF的Web中有以下CustomeBinding。配置文件

 <?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
          <bindings>
            <customBinding>
                <binding name="notSecureBinding">
                    <textMessageEncoding messageVersion="Soap12" />
                    <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
                <binding name="SecureBinding">
                   <textMessageEncoding messageVersion="Soap12" />
                   <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
                      binding="notSecureBinding"
                      bindingConfiguration="notSecureBinding"
                      contract="myNamespace.Contract1"
                      name="notSecureBinding" />
            <endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
                      binding="SecureBinding"
                      bindingConfiguration="SecureBinding"
                      contract="myNamespace.Contract1"
                      name="SecureBinding" />
        </client>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
          </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

请注意,我显式地将messageVersion设置为Soap12,但是当我在下面的代码中调用服务时,我得到一条错误消息,这似乎表明服务期望Soap11,我发现这很奇怪。

代码

 HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
 TextMessageEncodingBindingElement TextMessage = new TextMessageEncodingBindingElement() { MessageVersion = System.ServiceModel.Channels.MessageVersion.Soap12 };
 CustomBinding customBinding = new CustomBinding(TextMessage, httpTransport);
 EndpointAddress endpointAddress = new EndpointAddress("http://ServerName.myDomain.org/ADSearcher/Service1.svc");
 IService1 ADUser = new ChannelFactory<IService1>(customBinding, endpointAddress).CreateChannel();
 DataTable dt = ADUser.GetUserList(strUserName, strFirstName, strLastName, strEmail, domain);

如果我用"MessageVersion"调用上面的代码。Soap12" i get以下错误

内容类型application/soap+xml;Charset =utf-8被发送到期望text/xml的服务;charset = utf - 8。客户端和服务绑定可能不匹配。

但是如果我将MessageVersion设置为"MessageVersion. "Soap11"(现在不同于Soap11,我有web。配置绑定),代码执行成功,没有任何不匹配。

我不明白为什么。我倾向于使用messagversion。Soap12支持https

在WCF服务中支持Soap12

服务可能没有使用定义的配置(基于发布的代码)。如果"notSecureBinding"没有显式地分配给端点(通过bindingConfiguration)或设置为默认绑定,则服务不会使用它,并且您将获得(默认情况下)basicHttpBinding端点,如您所发现的,SOAP 1.1。

有几种方法可以解决这个问题。首先,你可以声明一个端点(如果你还没有端点的话)并分配绑定配置(这将在服务的配置文件中)。

<system.serviceModel>
   <bindings>
     <customBinding>
       <binding name="notSecureBinding">
         <textMessageEncoding messageVersion="Soap12" />
         <httpTransport maxReceivedMessageSize="2147483647" 
                        maxBufferSize="2147483647" />
       </binding>
    </customBinding>
  </bindings>
  <services>
    <service name="MyServiceName">
      <endpoint address="" binding="customBinding"
                bindingConfiguration="notSecureBinding"
                contract="MyService.IMyService" />
    </service>
  </services>
</system.serviceModel>

第二种方法是使配置成为默认配置并将其分配给给定的协议(方案),尽管如果您要有多个配置,这可能不是一个好的解决方案。

对于第二种方式,定义不带name属性的配置:

<system.serviceModel>
   <bindings>
     <customBinding>
       <binding>
         <textMessageEncoding messageVersion="Soap12" />
         <httpTransport maxReceivedMessageSize="2147483647" 
                        maxBufferSize="2147483647" />
       </binding>
    </customBinding>
  </bindings>
</system.serviceModel>

由于使用的是customBinding,因此还需要更新"部分中的<protocolMapping>以反映这一点:

<protocolMapping>
  <add binding="customBinding" scheme="http" />
</protocolMappings>

任何一种方法都将导致您的服务选择正确的绑定配置。