【问题标题】:Using multiple namespaces in WCF在 WCF 中使用多个命名空间
【发布时间】:2022-01-11 16:03:53
【问题描述】:

我有一个 C# 应用程序然后我必须给出如下请求的结果。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Body>
    <MyAppResponse xmlns="http://www.specificAddress.com">
      <MyAppResult xmlns:abc1="http://schemas.specificAddress.Responses" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <abc1:ResponseData xmlns:aef2="http://schemas.datacontract.org/Test.GenericMerchantServices.Classes.Domain">
          <aef2:ResponseCode>0000</aef2:ResponseCode>
          <aef2:ResponseDescription>Successful</aef2:ResponseDescription>
        </abc1:ResponseData>
      </MyAppResult>
    </MyAppResponse>
  </s:Body>
</s:Envelope>

在这种情况下,我使用 .NET 5 应用程序和 soapCore 在 .NET Core 中使用 soap。这是我的接口和派生类。

[ServiceContract]
public interface IMyAppService
{
    [OperationContract]
    MyAppResult MyApp(MyAppRequest request); 
}




public class MyAppService : IMyAppService
    {
        public MyAppResult MyApp(MyAppRequest request)
        { 
                return new MyAppResult()
                {
                    ResponseData = new ResponseData()
                    {
                        ResponseCode = "0000",
                        ResponseDescription = "Test "
                    }
                };
         }    
     }

我尝试使用数据注释更新命名空间,但我不知道如何更新前缀并按要求做出响应。这是我的课

    [XmlRoot(Namespace = "http://schemas.specificAddress.Responses")]
    public class MyAppResult
    {
        [XmlElement(ElementName = "ResponseData", Namespace = "http://schemas.datacontract.org/Test.GenericMerchantServices.Classes.Domain")]
        public ResponseData ResponseData { get; set; }
    }
     
    public class ResponseData
    {       
        public string ResponseCode { get; set; }
        public string ResponseDescription { get; set; }
    }

当我使用这个端点时,我得到了这个结果。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Body>
    <MyAppResponse xmlns="http://tempuri.org/">
      <MyAppResult xmlns="http://schemas.specificAddress.Responses" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <ResponseData xmlns="http://schemas.datacontract.org/Test.GenericMerchantServices.Classes.Domain">
          <ResponseCode>0000</ResponseCode>
          <ResponseDescription>Test </ResponseDescription>
        </ResponseData>
      </MyAppResult>
    </MyAppResponse>
  </s:Body>
</s:Envelope>

问题是如何在我的项目中添加 ns 和自定义前缀以及如何在 myappresponse 上添加自定义命名空间。

【问题讨论】:

    标签: c# xml wcf soap


    【解决方案1】:

    要更改 WCF 服务的命名空间,您需要将 Namespace 属性应用于服务协定接口上的 ServiceContractAttribute。详情请见http://blog.rebuildall.net/2010/11/10/WCF_service_namespaces
    关于添加前缀可以看看这个文档。
    XML Serialization and namespace prefixes
    C# WCF Global Namespaces - Royal Mail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 2012-07-31
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      相关资源
      最近更新 更多