Home > Mobile >  Using multiple namespaces in WCF
Using multiple namespaces in WCF

Time:02-17

I have an application in C# Then i have to give a requested result as below.

<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>

In this case i am using a .NET 5 app and soapCore to work with soap in .NET Core. Here ise my interface and derived 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 "
                    }
                };
         }    
     }

I've tried to update namespaces with data annotiations but i don't know how can i update prefix and make response as requested. Here is my 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; }
    }

When i use this endpoint i get this result.

<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>

The question is how can i add ns and custom prefix to my project and how can i add custom namespace on myappresponse.

CodePudding user response:

To change the namespace of a WCF service, you need to apply the Namespace attribute to the ServiceContractAttribute on the service contract interface. Details can be found at http://blog.rebuildall.net/2010/11/10/WCF_service_namespaces
About adding prefixes you can take a look at this document.
XML Serialization and namespace prefixes
C# WCF Global Namespaces - Royal Mail

  • Related