【问题标题】:How do I associate Xml Schema with XmlDocument type using WCF DataContract如何使用 WCF DataContract 将 Xml Schema 与 XmlDocument 类型相关联
【发布时间】:2014-06-20 21:37:51
【问题描述】:

我的任务是创建一个 WCF 服务操作,该操作将使用 XSLT 将 XML 从一种形式转换为另一种形式。当使用普通的旧 asmx Web 服务执行此操作时,我使用了 XmlDocument 类型的输入参数和相同的返回类型。

输入 XmlDocument 需要具有关联的架构和命名空间。事实上,当我们的测试人员使用 SOAP UI 针对 asmx Web 服务进行测试时,他们会收到以下错误 -

错误:无法导入命名空间“http://ACORD.org/Standards/Life/2”中的类型“TXLife_Type”。属性必须是可选的,并且来自命名空间“http://schemas.microsoft.com/2003/10/Serialization/”。要么更改架构,以便类型可以映射到数据协定类型,要么使用 ImportXmlType 或使用不同的序列化程序。

我认为我真正需要的是 3 个命名空间,一个用于 ServiceContract,一个用于输入 XML 参数,一个用于返回的输出 XML。我该怎么做?

【问题讨论】:

  • 我想我可能已经弄清楚了如何将命名空间与 XML 文档类型相关联。我正在使用带有命名参数 WrapperNamespace 的 MessageContract 属性。但是,当我尝试在客户端中使用它时遇到问题,因为生成的客户端代码与预期的不匹配。客户端方法不接受消息并返回消息,而是不接受任何内容并返回 void。为什么生成的客户端代码与服务合约不匹配?
  • code // 这是来自 IService1.cs [ServiceContract(Namespace="itsawonderfullife.com/LifeSOA/Services/"), XmlSerializerFormat] public interface IService1 { [OperationContract, XmlSerializerFormat] Output TX103toLifeCommTest(InputTx103 tx103); } [MessageContract(WrapperNamespace = "ACORD.org/Standards/Life/2"), XmlSerializerFormat] 公共类 InputTx103 { 公共 XmlDocument TXLife; } [MessageContract, XmlSerializerFormat] 公共类输出 { 公共 XmlDocument TXLife; } code
  • code// 这是来自客户端生成的代码 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] Output IService1.TX103toLifeCommTest(InputTx103 request) { return base.Channel. TX103toLifeCommTest(请求); } public void TX103toLifeCommTest() { InputTx103 inValue = new InputTx103();输出 retVal = ((IService1)(this)).TX103toLifeCommTest(inValue); } code

标签: xml web-services wcf soapui


【解决方案1】:

用于服务契约接口。我用过这个-

namespace MyNamespace
{
[ServiceContract(Namespace = "http://ItsAWonderfulLife.com/LifeSOA/Services/")]
public interface TX103toEAppXmlTransformServiceContract
{
    [OperationContract, XmlSerializerFormat]
    Output iGOTX103toLifeCommTest(Input input);
}

[MessageContract(WrapperNamespace = "http://ACORD.org/Standards/Life/2")]
public class Input
{
    [MessageBodyMember]
    public XElement TXLife { get; set; }
}

[MessageContract]
public class Output
{
    [MessageBodyMember]
    public XElement eAppXML { get; set; }
}  

}

对于服务实现 -

[ServiceBehavior(Namespace = "http://ItsAWonderfulLife.com/LifeSOA/Services/")]
public class TX103toEAppXmlTransform : TX103toEAppXmlTransformServiceContract
{
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多