本文介绍了支持在WCF MTOM都和文本作为MessageEncoding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WCF service, which has a method with the following signature:

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

它的工作与对象同时作为返回类型和最后一个参数的原因,是因为它应该可以通过任何类型作为参数,并返回任何类型。

The reason it's working with objects as both the return type and last argument, is because it should be possible to pass any type as argument and return any type.

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

Anyway, I'm passing an object, which contains the following property:

public byte[] Photo { get; set; }

要启用大的消息,我想开始使用MTOM,而我是previously使用纯文本作为MessageEncoding类型。

To enable large messages, I'd like to start using Mtom, while I was previously using plain-text as MessageEncoding type.

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

Problem is, I want it to be backwards compatible, so current already-configures clients should keep using plain-text encoding, while new clients should be able to use Mtom via the web.config.

我的问题是:是否有可能继续使用纯文本作为MessageEncoding默认情况下(现有客户),并提供MTOM编码以及并排侧

My question is: is it possible to keep using plain-text as MessageEncoding by default (existing clients) and offer Mtom encoding as well side-by-side?

我已经尝试了一些事情的配置,如定义具有不同的绑定的配置多个端点,但我不能得到它的工作:

I've tried some things with the configuration, like defining multiple endpoints with different binding-configurations, but I can't get it to work:

服务器

<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,我会得到一个异常:

If I don't set Mtom messageEncoding @ the client, everything works via plain-text. But when I set it to use Mtom, I'll get an exception:

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'..

任何人能帮助我吗? : - )

Anyone able to help me out? :-)

推荐答案

如果你想添加一个新的端点(较新的客户端),端点需要在不同的地址。既然你没有得到任何错误,我想你在名称不正确的名称的属性&LT;服务&GT; 元素。请记住,name属性应包含的完全合格的名称的服务类的。如果你的服务类是在命名空间 InfoCaster.SocialProxy ,您的服务配置应该象下面这样定义:

If you want to add a new endpoint (for newer clients), that endpoint needs to be in a different address. Since you didn't get any error, I imagine you have an incorrect name in the name attribute of the <service> element. Remember that the name attribute should contain the fully-qualified name of the service class. If your service class is at the namespace InfoCaster.SocialProxy, your service configuration should be defined like below:

<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响应返回,就可以仍然这样做。你需要一个定制的连接codeR,我在后的<一个写出了一部href=\"http://blogs.msdn.com/b/carlosfigueira/archive/2011/02/16/using-mtom-in-a-wcf-custom-en$c$cr.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/carlosfigueira/archive/2011/02/16/using-mtom-in-a-wcf-custom-en$c$cr.aspx.

Now, if you want a single endpoint which can support both text and MTOM in a way that clients sending text receive a text response, and clients which send MTOM receive a MTOM response back, you can still do it. You'll need a custom encoder, and I wrote one in the post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/02/16/using-mtom-in-a-wcf-custom-encoder.aspx.

这篇关于支持在WCF MTOM都和文本作为MessageEncoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:10