控制WCF消息正文序列化 [英] Controlling WCF Message Body serialization

查看:110
本文介绍了控制WCF消息正文序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要创建一个WCF服务,该服务将模仿某些第三方服务.该服务为它们提供了非常复杂的消息和包装,因此我将在说明中对其进行简化.
我具有该服务的WSDL并创建了代理类.但是这里出现了第一个问题:代理中的所有方法都有
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")] 因此,不可能使用多种方法来创建WCF服务:代理中的每个方法都必须使用每个方法,因为每个方法必须具有唯一的Action.我认为第三方服务具有一种处理所有请求的方法.然后,我在RequestTypeBase和ResponceTypeBase上使用所需的KnownType属性创建了这种方法.所有代理类方法都有一个类型为参数的类型,该参数派生自RequestTypeBase. 这是主要的问题和疑问::当WCF服务尝试反序列化消息正文时,它将引发异常,指出期望的elementName为"Process"(处理所有请求的我的宏方法的名称)但是现有的elementName是"RequestType1"(具有必须作为参数传递到"Process"方法的数据的名称类). 那我怎么能收到这样的消息呢?在WCF中是否有一些属性不要求methodName作为消息主体的根?而且我什至不知道WCF在那里已经知道调用什么方法的情况下需要那个MethodName是什么?看起来像带有动作说明的冗余.


I need to create a WCF service that will emulate some third-party service. That service has very complicated messages and wrappers for them, so I will simplify them in description.
I have WSDL of that service and created proxy class. But here the first problem occurs: all methods in proxy have
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")] So it is impossible to create WCF service with many methods: each for one method in proxy because every method must have unique Action. I think that third-party service has one method that handles all requests. And I created such method with needed KnownType attributes on RequestTypeBase and ResponceTypeBase. All proxy-class methods have one parameter of type, derived from RequestTypeBase. And here is the main problem and question: when WCF service tries to deserialize message body, it throws an exception saying that expected elementName is "Process" (the name of my mega-method that processes all requests) but existing elementName is "RequestType1" (the name class with data that must be passed to "Process" method as parameter). So how can I receive such message?? Is there some attribute in WCF to not require the methodName as root of Message body? And I even not understand for what does WCF need that MethodName there if he already knows what method is called? Looks like redundancy with action specification.

也许通过WCF成功处理的简单MessabeBody示例将有助于理解我的意思:

Maybe simple MessabeBody example that is successfully processing by WCF, will help to understand what I mean:

<s:Body>
  <TestMethod xmlns="someNamespace">
    <x>1</x>
    <str>param2</str>
  </TestMethod>
</s:Body>

推荐答案

您可以使用通用服务合同"在服务端完全跳过WCF反序列化:

You could skip WCF deserialization completely on the service side by using the "universal service contract":

[ServiceContract]
public interface IUniversalRequestResponseContract
{
    [OperationContract(Action="*", ReplyAction="*")]
    Message ProcessMessage(Message msg);
}

然后使用接收到的Message实例自行处理反序列化.

and then handle deserialization yourself working with the Message instance received.

如果您出于测试目的而编写一些外部服务的存根仿真(我猜是这样),那么无论如何这都是一个好方法,因为您可以精确控制响应中发送的内容.

If you are writing a stub emulation of some external service for testing purposes (I'm guessing), that is a good approach anyway because you can control exactly what is sent in the response.

这篇关于控制WCF消息正文序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆