在WCF服务中支持Soap12

时间:2015-05-19 15:38:35

标签: c# wcf soap

我在WCF的Web.config文件中有以下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);

如果我用&#34; MessageVersion.Soap12&#34;来调用上面的代码我得到以下错误

  

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

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

我不明白为什么。我的偏好是使用带有https支持的MessageVersion.Soap12。

1 个答案:

答案 0 :(得分:2)

可能(基于发布的代码)服务未使用定义的配置。如果未将“notSecureBinding”显式分配给端点(通过bindingConfiguration)或设置为默认绑定,则服务将不会使用它,并且您将获得(默认情况下){{1}正如您所发现的那样,端点就是SOAP 1.1。

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

basicHttpBinding

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

对于第二种方式,定义没有<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

由于您正在使用customBinding,因此您还需要更新“”部分中的<system.serviceModel> <bindings> <customBinding> <binding> <textMessageEncoding messageVersion="Soap12" /> <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> </binding> </customBinding> </bindings> </system.serviceModel> 以反映这一点:

<protocolMapping>

这两种方法都会导致您的服务获得正确的绑定配置。