我有一个简单的回显服务,我在其中定义了一个操作方法和一对请求/响应类型:
[ServiceContract(Name = "EchoService",
Namespace = "http://example.com/services",
SessionMode = SessionMode.NotAllowed)]
public interface IEchoService
{
[OperationContract(IsOneWay = false,
Action = "http://example.com/services/EchoService/Echo",
ReplyAction = "http://example.com/services/EchoService/EchoResponse")]
EchoResponse Echo(EchoRequest value);
}
数据类型:
[Serializable]
[DataContract(Namespace = "http://example.com/services/EchoService",
Name = "EchoRequest")]
public class EchoRequest
{
public EchoRequest() { }
public EchoRequest(String value)
{
Value = value;
}
[DataMember]
public String Value { get; set; }
}
[Serializable]
[DataContract(Namespace = "http://example.com/services/EchoService",
Name = "EchoResponse")]
public class EchoResponse
{
public EchoResponse() { }
public EchoResponse(String value)
{
Value = value;
}
[DataMember]
public String Value { get; set; }
}
在 EchoRequest 实例上调用 Message.CreateMessage() 会产生:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<EchoRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.com/services/EchoService">
<Value>hello, world!</Value>
</EchoRequest>
</s:Body>
</s:Envelope>
...这正是我想要的。但是,服务似乎希望消息正文进一步包装在另一个 XML 元素中,如下所示:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<Echo xmlns="http://example.com/services">
<EchoRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.com/services/EchoService">
<Value>hello, world!</Value>
</EchoRequest>
</Echo>
</s:Body>
</s:Envelope>
更新: 感谢 Mark 的回复,我在请求/响应类型上探索了 MessageContract 而不是 DataContract。这似乎更接近我想要的,但现在它走得太远了,并且不期望外部类型元素“EchoRequest”。
这令人困惑,因为 Message.CreateMessage 似乎以某种方式始终生成正确的 XML,因此它显然使用了一些默认序列化,我想将服务配置为接受。我只是误解了 Message.CreateMessage 的工作原理吗?