我们有一个现有的 SOAP Web 服务接口(interface),我们希望使用 WCF 为新应用程序实现该接口(interface)。除了一个小细节外,这似乎工作正常。函数返回类型的 XML 命名空间必须不同于 Web 服务本身的 XML 命名空间。对于我的生活,我无法让它工作。
我用一个小示例项目重现了同样的问题。 WCF 接口(interface):
[XmlSerializerFormat]
[ServiceContract(Namespace = "urn:outer-namespace")]
public interface IService1
{
[OperationContract]
MyClass DoStuff(int value);
}
[Serializable]
public class MyClass
{
[XmlElement(ElementName = "DataString")]
public string MyString { get; set; }
}
网络服务实现:
public class Service1 : IService1
{
public MyClass DoStuff(int value)
{
return new MyClass { MyString = "Wooh!" };
}
}
来自此网络服务的响应随后被序列化为: (省略 SOAP 内容)
<DoStuffResponse xmlns="urn:outer-namespace">
<DoStuffResult>
<DataString>Wooh!</DataString>
</DoStuffResult>
</DoStuffResponse>
但是我们希望
我试过在接口(interface)函数或 Web 服务函数上添加一个 [return: XmlElement(...)],但那不行。 MyClass 类定义中的 [XmlType] 或 [XmlRoot] 也不起作用。
有谁知道如何更改作为 WCF Web 服务函数返回值的对象的序列化 XML 命名空间(或元素名称)?
最佳答案
经过数天的搜索和尝试数十种推荐的解决方案;我终于能够让 WCF 停止强制将 Result
附加到 Web 服务方法名称的包装器容器名称。诀窍是将以下装饰器属性添加到 Web 服务接口(interface):
[return:MessageParameter(Name = "whatIWantItNamed")]
此属性应直接放置/位于接口(interface)中 [OperationContract]
属性之后(并且恰好在实际方法 stub 之前)。
(我还需要将 XmlSerializerFormat
属性添加到所有 ServiceContract
和 OperationContract
属性。)
关于c# - 如何自定义 WCF XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/649913/