如何将WCF SOAP消息当选为生成类
SOAP响应消息:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:stef">
<soap:Body>
<tns:ConvertResponse>
<tns:Result>abc</tns:Result>
</tns:ConvertResponse>
</soap:Body>
</soap:Envelope>
生成的代码,该代码在添加Service-Reference( sippet )时生成的代码:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName = "ConvertResponse", WrapperNamespace = "urn:stef", IsWrapped = true)]
public partial class ConvertResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "urn:stef", Order = 0)]
public string Result;
public ConvertResponse()
{
}
public ConvertResponse(string Result)
{
this.Result = Result;
}
}
我使用以下通用代码将此SOAP字符串对Convertresponse
进行了启用。对象:
private static T Deserialize<T>(string xml)
{
var stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
var message = Message.CreateMessage(XmlReader.Create(stream), int.MaxValue, MessageVersion.Soap11);
var importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:stef");
var mapping = importer.ImportTypeMapping(typeof(T));
var xmlSerializer = new XmlSerializer(mapping);
return (T)xmlSerializer.Deserialize(message.GetReaderAtBodyContents());
}
解决方案基于 https://stackoverflow.com/a/a/43901543/17218587
上面的 因为contrestresponse
类没有 public result
的属性,所以这就是原因result
在求职后无效。
所以我的问题是,是否有一种方法可以将此XML肥皂消息划分到(生成的)类中?
Soap Response message:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:stef">
<soap:Body>
<tns:ConvertResponse>
<tns:Result>abc</tns:Result>
</tns:ConvertResponse>
</soap:Body>
</soap:Envelope>
Generated code, which is generated by VS when adding a service-reference (snippet) :
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName = "ConvertResponse", WrapperNamespace = "urn:stef", IsWrapped = true)]
public partial class ConvertResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "urn:stef", Order = 0)]
public string Result;
public ConvertResponse()
{
}
public ConvertResponse(string Result)
{
this.Result = Result;
}
}
I used the following generic code to deserialize this soap string into the ConvertResponse
object:
private static T Deserialize<T>(string xml)
{
var stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
var message = Message.CreateMessage(XmlReader.Create(stream), int.MaxValue, MessageVersion.Soap11);
var importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:stef");
var mapping = importer.ImportTypeMapping(typeof(T));
var xmlSerializer = new XmlSerializer(mapping);
return (T)xmlSerializer.Deserialize(message.GetReaderAtBodyContents());
}
The solution above is based on https://stackoverflow.com/a/43901543/17218587
But this does not work because the ConvertResponse
class does not have a public property for Result
, so that's the reason Result
is null after deserialization.
So my question is if there is a way to deserialize this xml-soap message into that (generated) class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道Stef Heyenrath是否得到了答案,但是这里的问题是,SoaprefreflectionImporter和XMLSerializer旨在与用XMLSerialializerFormat标记的消息一起使用。但是,由于它具有MessageCecntract,因此我们也不能使用DataContractSerialializer。
I also needed to deserialize messages, and after much fiddling in google, found the answer here:
?
I don't know if Stef Heyenrath got the answer to this, but the question here is, SoapReflectionImporter and XmlSerializer are meant to be used with messages marked with XmlSerializerFormat. But, since it has MessageContract, we cannot use DataContractSerializer either.
I also needed to deserialize messages, and after much fiddling in google, found the answer here: WCF DataContractSerializer doesn't pick up contract attributes... why not?
So, according to blowdart, your code would be something like this: