简体   繁体   English

WCF - 自定义客户端请求/响应XML

[英]WCF - Customized client request/response XML

I am trying to consume a vendor provided AXIS Web service with WCF client. 我正在尝试使用WCF客户端使用供应商提供的AXIS Web服务。 The service expects to have the request/response element <TXLife> as the root element of the SOAP body (no operation element wrapping it). 该服务期望将请求/响应元素<TXLife>作为SOAP主体的根元素(没有包装它的操作元素)。 I'm using the XmlSerializer because my data contract has some custom ACORD schema idiosyncrasies. 我正在使用XmlSerializer,因为我的数据协定有一些自定义的ACORD模式特性。 For example, the server wants to see the following (...and yes, "service" is the name of the operation...): 例如,服务器想要看到以下内容(...和是,“service”是操作的名称...):

...<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><TXLife><TXLifeRequest xmlns="">...    

My client is generating the XML with the operation serialized as a wrapping element like this: 我的客户端正在生成XML,并将操作序列化为包装元素,如下所示:

...<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><service xmlns="urn:example.servicecontract"><TXLife><TXLifeRequest xmlns="">...

With the "extra" tag indicating the operation in the request the service is not able to handle the request and errors out. 使用“extra”标记指示请求中的操作,服务无法处理请求和错误输出。 If I remove the <service> tag the Web service happily processes the request. 如果我删除<service>标记,Web服务将愉快地处理请求。

Unfortunately, the service also sends the response down with the unwrapped <TXLife> tag as the root element: 不幸的是,该服务还会以解包的<TXLife>标记作为根元素发送响应:

...<soapenv:Body><TXLife xmlns=""><TXLifeResponse>...

My de-serializer is not handling the response properly and I get a null object back. 我的反序列化器没有正确处理响应,我得到一个null对象。 I assume because my client is expecting a service operation response wrapper tag and not getting one. 我假设因为我的客户端期望服务操作响应包装器标签而不是一个。 I don't get much help from the debugger at the de-serialization level. 在反序列化级别,我没有得到调试器的帮助。

I was thinking about implementing the IClientMessageFormatter or even the IClientMessageInspector to modify the request/response (eg Remove the operation tag from the request message and add a response tag to the response message). 我正在考虑实现IClientMessageFormatter甚至IClientMessageInspector来修改请求/响应(例如,从请求消息中删除操作标记并在响应消息中添加响应标记)。 I know the Formatter is injected as an OperationalBehavior but I'm not sure where the MessageInspector fits into the stack. 我知道Formatter是作为OperationalBehavior注入的,但我不确定MessageInspector在哪里适合堆栈。 Maybe I'm going about this the wrong way... Any insight or suggestions would be appreciated. 也许我会以错误的方式解决这个问题......任何见解或建议都会受到赞赏。 Forgive me, this is my first attempt at WCF services and I'm slowly feeling my way through. 原谅我,这是我第一次尝试WCF服务,我正在慢慢地感受我的路。 Unfortunately, everything about this service seems to be "custom". 不幸的是,关于这项服务的一切似乎都是“定制的”。

My Service Contract: 我的服务合同:

[XmlSerializerFormat]
[ServiceContract(Namespace="urn:example.servicecontract")]
public interface IPayoutServiceContract
{
    [OperationContract]
    TXLife service([MessageParameter(Name = "TXLife")]TXLife request);
}

A portion of the service WSDL: 服务WSDL的一部分:

<wsdl:types><schema targetNamespace="urn:Tx103Service" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><import id="tx" namespace="http://ACORD.org/Standards/Life/2" schemaLocation="../acord/TXLife2.8.92.xsd" /></schema></wsdl:types><wsdl:message name="serviceRequest"><wsdl:part element="tx:TXLife" name="acordRequest" /></wsdl:message><wsdl:message name="serviceResponse"><wsdl:part element="tx:TXLife" name="acordResponse" /></wsdl:message><wsdl:portType name="LifeWebService"><wsdl:operation name="service"><wsdl:input message="impl:serviceRequest" name="serviceRequest" /><wsdl:output message="impl:serviceResponse" name="serviceResponse" /></wsdl:operation></wsdl:portType>

Update: 更新:

I first tried using the MessageContract(isWrapped=false) decorator on the proxy class (the interface disallows it). 我首先尝试在代理类上使用MessageContract(isWrapped=false)装饰器(接口禁止它)。 That did nothing. 那什么都没做。 I also tried flavors of BodyStyle = WebMessageBodyStyle.Bare , also nothing. 我也试过BodyStyle = WebMessageBodyStyle.Bare ,也没什么。 I assume this is due to the XMLSerializer I'm using. 我假设这是由于我正在使用的XMLSerializer。 It seems to me that there is no easy way to "decorate" my way around this issue. 在我看来,没有简单的方法来“装饰”我的方式围绕这个问题。

BTW: My service contract, data contract, and proxy client are all in separate projects per this recommendation which sounded solid to me: blog post by Miguel Castro BTW:根据这个建议,我的服务合同,数据合同和代理客户都在不同的项目中,这听起来很可靠: Miguel Castro的博客文章

Update2: UPDATE2:

I created Request/Response wrapper classes decorated with MessageContract/MessageBodyMember tags. 我创建了使用MessageContract / MessageBodyMember标记修饰的Request / Response包装类。 Now the XML is generated as expected. 现在,XML按预期生成。 Still getting the null object in the response... 仍然在响应中得到null对象......

Update3: UPDATE3:

The "null" objects in my response were actually present in the XML response, but not being de-serialized because the serializer was looking for them as qualified objects. 我的响应中的“null”对象实际上存在于XML响应中,但没有被反序列化,因为序列化程序将它们作为限定对象进行查找。 I changed them to non-qualified and my objects showed up just fine after that. 我将它们改为不合格,之后我的对象就显得很好了。

您是否尝试使用IsWrapped = false而不是使用[MessageParameter]的MessageContract?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM