支持Mtom和Text作为WCF中的MessageEncoding

时间:2012-04-12 17:42:53

标签: asp.net wcf iis wcf-binding mtom

我有一个WCF服务,它有一个带有以下签名的方法:

object GetCommand(Guid apiKey, SocialService service, string name, object argument);

它使用对象作为返回类型和最后一个参数的原因是因为应该可以将任何类型作为参数传递并返回任何类型。

无论如何,我正在传递一个包含以下属性的对象:

public byte[] Photo { get; set; }

要启用大型消息,我想开始使用Mtom,而我以前使用纯文本作为MessageEncoding类型。

问题是,我希望它向后兼容,因此当前已经配置的客户端应该继续使用纯文本编码,而新客户端应该能够通过web.config使用Mtom。

我的问题是:默认情况下是否可以继续使用纯文本作为MessageEncoding(现有客户端)并同时提供Mtom编码?

我已经尝试了一些配置,比如使用不同的绑定配置定义多个端点,但我无法让它工作:

服务器

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpTextBinding_SocialProxy" />
            <binding name="BasicHttpMtomBinding_SocialProxy" maxReceivedMessageSize="5242880" messageEncoding="Mtom">
                <readerQuotas maxStringContentLength="655360" maxArrayLength="1310720" maxNameTableCharCount="1310720" maxBytesPerRead="327680" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service name="SocialProxyService">
            <endpoint name="BasicEndpoint_SocialProxy" address="" contract="InfoCaster.SocialProxy.ISocialProxy" binding="basicHttpBinding" bindingConfiguration="BasicHttpTextBinding_SocialProxy" />
            <endpoint name="MtomEndpoint_SocialProxy" address="" contract="InfoCaster.SocialProxy.ISocialProxy" binding="basicHttpBinding" bindingConfiguration="BasicHttpMtomBinding_SocialProxy" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true" />
                <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

客户端

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_SocialProxy" messageEncoding="Mtom" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://socialproxy.local/socialproxy.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SocialProxy"
            contract="Webservice.SocialProxy" name="BasicHttpBinding_SocialProxy" />
    </client>
</system.serviceModel>

问题是:

如果我没有在客户端设置Mtom messageEncoding @,那么一切都可以通过纯文本工作。但是当我设置它使用Mtom时,我会得到一个例外:

The remote server returned an error: (415) Cannot process the message because the content type 'multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="uuid:65a6b418-8eb3-4c76-b4c0-ea3486a56892+id=2";start-info="text/xml"' was not the expected type 'text/xml; charset=utf-8'..

有人能帮帮我吗? : - )

1 个答案:

答案 0 :(得分:4)

如果要添加新端点(对于较新的客户端),该端点需要位于不同的地址。由于您没有收到任何错误,我想您name元素的<service>属性中的名称不正确。请记住,name属性应包含服务类的完全限定名称。如果您的服务类位于命名空间InfoCaster.SocialProxy,则应定义您的服务配置,如下所示:

<services> 
    <service name="InfoCaster.SocialProxy.SocialProxyService"> 
        <endpoint name="BasicEndpoint_SocialProxy" address="" contract="InfoCaster.SocialProxy.ISocialProxy" binding="basicHttpBinding" bindingConfiguration="BasicHttpTextBinding_SocialProxy" /> 
        <endpoint name="MtomEndpoint_SocialProxy" address="newClients" contract="InfoCaster.SocialProxy.ISocialProxy" binding="basicHttpBinding" bindingConfiguration="BasicHttpMtomBinding_SocialProxy" /> 
    </service> 
</services> 

客户会有像

这样的东西
<client>  
    <endpoint address="http://socialproxy.local/socialproxy.svc/newClients"  
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SocialProxy"  
        contract="Webservice.SocialProxy" name="BasicHttpBinding_SocialProxy" />  
</client>  

现在,如果你想要一个端点,它可以支持文本和MTOM,发送文本的客户端收到文本响应,发送MTOM的客户端收到MTOM响应,你可以仍然这样做。您需要一个自定义编码器,我在http://blogs.msdn.com/b/carlosfigueira/archive/2011/02/16/using-mtom-in-a-wcf-custom-encoder.aspx的帖子中写了一个。