gpt4 book ai didi

c# - 从文件系统反序列化 SOAP 消息

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:33 25 4
gpt4 key购买 nike

我之前连接到通过 WSDL 文件定义的 WCF 端点。一切都很棒,我通过以下方式检索了数据;

var client = new ElementRetrieval_RPCClient();
var header = new header();
var elements = client.getAllElements(ref header, new getAllElementsRequest()).meList;

// Loop over elements.

但是,由于荒谬的政策,我的应用程序现在必须解析来自文件的 SOAP 响应(并依赖于第 3 方为我发出 SOAP 请求,并将响应转储到文件以供我收集)。

我试过了;

var converter = TypedMessageConverter.Create(typeof(getAllElementsResponse), "getAllElementsResponse", "", new XmlSerializerFormatAttribute());
var reader = XmlTextReader.Create(file);
var message = Message.CreateMessage(MessageVersion.Soap11, "getAllElementsResponse", reader);
var obj = (getAllElementsResponse) converter.FromMessage(message);

...但这会留下 obj 的属性成为null (尽管没有引发/记录错误)。我也试过:

var reader = XmlTextReader.Create(file);
var message = Message.CreateMessage(MessageVersion.Soap11, "getAllElementsResponse", reader);
var obj = message.GetBody<getAllElementsResponse>();

...但这失败了 SerializationException :

Additional information: Expecting element 'getAllElementsResponse' from namespace 'http://schemas.datacontract.org/2004/07/ElementRetrieval'.. Encountered 'Element' with name 'getAllElementsResponse', namespace 'http://www.example.org/mri/xsd/mer/v1'.

...这里,“Encountered”命名空间是正确的;但我不知道怎么说GetBody<T>() 期待那个命名空间?

getAllElementsResponse类(由 WCF 自动生成)如下所示(如果属性很重要):

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class getAllElementsResponse {

[System.ServiceModel.MessageHeaderAttribute(Namespace="http://www.example.org/fmw/xsd/hdr/v1")]
public ElementRetrieval.header header;

[System.ServiceModel.MessageBodyMemberAttribute(Name="getAllElementsResponse", Namespace="http://www.example.org/mri/xsd/mer/v1", Order=0)]
public ElementRetrieval.MultipleMeObjectsResponseType getAllElementsResponse1;

public getAllElementsResponse() {
}

public getAllElementsResponse(ElementRetrieval.header header, ElementRetrieval.MultipleMeObjectsResponseType getAllElementsResponse1) {
this.header = header;
this.getAllElementsResponse1 = getAllElementsResponse1;
}
}

... 和 getAllElements方法具有以下属性:

// CODEGEN: Generating message contract since the operation getAllElements is neither RPC nor document wrapped.
[System.ServiceModel.OperationContractAttribute(Action="getAllElements", ReplyAction="*")]
[System.ServiceModel.FaultContractAttribute(typeof(ElementRetrieval.getAllElementsException), Action="getAllElements", Name="getAllElementsException", Namespace="http://www.example.com/mri/xsd/mer/v1")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxx))]

谁能找出为什么这些方法都不起作用,并提出修复/解决方法的建议?

最佳答案

GetBody<> 操作在内部使用 DataContractSerializer,因此您必须添加具有特定命名空间的 DataContract 属性。

因为如果你不添加它,就会使用默认的(默认情况下 CLR 命名空间映射到 http://schemas.datacontract.org/2004/07/Clr.Namespace 这就是你得到异常的原因。

Additional information: Expecting element 'getAllElementsResponse' from namespace 'http://schemas.datacontract.org/2004/07/ElementRetrieval'.. Encountered 'Element' with name 'getAllElementsResponse', namespace 'http://www.example.org/mri/xsd/mer/v1'.

尝试这样的事情:

[MessageContract(IsWrapped=false)]
[DataContract(Namespace = "http://www.example.org/fmw/xsd/hdr/v1")
public partial class getAllElementsResponse
{
[DataMember]
[MessageHeader(Namespace="http://www.example.org/fmw/xsd/hdr/v1")]
public ElementRetrieval.header header;

[DataMember]
[MessageBodyMember(Name="getAllElementsResponse", Namespace="http://www.example.org/mri/xsd/mer/v1", Order=0)]
public ElementRetrieval.MultipleMeObjectsResponseType getAllElementsResponse1;
}

希望对你有帮助

关于c# - 从文件系统反序列化 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com