簡體   English   中英

在 WCF 中使用多個命名空間

[英]Using multiple namespaces in WCF

我在 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 一起使用。 這是我的接口和派生的 class。

[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 "
                    }
                };
         }    
     }

我嘗試使用數據注釋更新命名空間,但我不知道如何更新前綴並按要求做出響應。 這是我的 class

    [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 上添加自定義命名空間。

要更改 WCF 服務的命名空間,您需要將 Namespace 屬性應用到服務合約接口上的 ServiceContractAttribute。 詳細信息可以在http://blog.rebuildall.net/2010/11/10/WCF_service_namespaces找到
關於添加前綴,您可以查看此文檔。
XML 序列化和命名空間前綴
C# WCF 全局命名空間 - 英國皇家郵政

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM