本文介绍了使用 MessageContract 属性时出现问题:异常 ->来自命名空间“http://schemas.xmlsoap.org/soap/envelope/"的结束元素“Body"预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的 WSE3 Web 服务移至 WCF.但是客户端是WSE3客户端.

I am moving my WSE3 web services to the WCF. But the client is WSE3 client.

所有操作契约都返回一个 MessageContract 类的实例.这适用于 2 个操作,但对于同一服务合同的一个操作以某种方式失败.错误是:

All the Operation Contracts return an instance of the MessageContract classes. This works for 2 operations but somehow fails for one operation of the same service contract. The error is:

签名或解密无效.

当我查看 WCF 跟踪文件时,我发现了以下内容:

When I look into WCF trace file, I found the following:

The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'MyOperationName'. End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'MyOperationName' from namespace 'urn:MyProject:MyModule:2006:04:MyAuthorizationModule'.

我的观察是,当我使用 XmlRoot 属性来装饰响应类(而不是使用 MessageContract 属性)时,我没有得到这个异常.但是,响应对象不能反序列化.即我可以在输入跟踪中看到 XML 响应,但由于预期的 XML 结构不匹配,服务调用在客户端返回 null.

My observation is, when I use XmlRoot attribute to decorate response class (instead of using MessageContract attribute), I do not get this exception. However, response object cannot be deserialized. i.e. I can see the XML response in input trace but since the expected XML structure does not match, service call returns null at client side.

MessageContract 类只有一个公共属性 (MessageBodyMember),它返回另一个用 XmlRoot 属性修饰的类的实例.这个类(用 xmlRoot 装饰)有一个属性,它给出了一些实体类的对象的 Collection,其中有 XmlElement 属性.

The MessageContract class has only one public property (MessageBodyMember) which returns an instance of another class which is decorated with the XmlRoot attribute. This class (which is decorated with xmlRoot) has a property which gives the Collection of objects of some entity class which has XmlElement properties in it.

我需要检查/验证哪些所有内容?如果需要,我可以提供课程代码片段.

Which all things I need to check/verify? I can provide class code snippets if required.

推荐答案

响应中使用的 MessageContract 没有问题.问题出在 OperationContract 的输入参数上.

There was no problem with the MessageContract which was used in response. The problem was with the input parameter to the OperationContract.

当我查看旧的 WSE3 Web 服务代理方法 (WebMethod) 并在 WCF 服务中为其创建 OperationContract 时,OparationContract我创建的不接受任何参数.

When I looked at the old WSE3 web service proxy method (WebMethod) and created the OperationContract for it in WCF service, the OparationContract I created did not accept any parameter.

在调查此问题时,我使用 svcutil.exe 从旧 WSE3 服务的 WSDL 创建 .NET 类.当我查看特定的 OperationContract 时,我知道我需要创建一个 MessageContract,它将用作 OperationContract 的请求参数.所以我创建了一个没有任何 MessageBodyMemberMessageContract.当我使用它时,问题得到了解决.

While investigating this issue, I used the svcutil.exe to create .NET classes from the WSDL of the old WSE3 service. When I looked into the specific OperationContract I came to know that I need to create a MessageContract which will be used as request parameter to the OperationContract. So I created a MessageContract without any MessageBodyMember. When I used it, the problem got resolved.

显然,如果我们将 OperationContract 签名与 ASMX WebMethod 签名进行比较,它们不匹配,因为我们引入了输入参数.但这有效.我不知道如何以及为什么.如果有人解释它为什么有效,那就太好了.

Obviously, if we compare the OperationContract signature with the ASMX WebMethod signature, they dont match since we have introduced input parameter. But this works. I do not know how and why. It would be great if someone explains why it works.

这篇关于使用 MessageContract 属性时出现问题:异常 ->来自命名空间“http://schemas.xmlsoap.org/soap/envelope/"的结束元素“Body"预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:07