Search code examples
c#xmlapi.net-coresoap

Set SOAP Namepace in .net Core 6 with SOAPCore


I am trying to create a SOAP API, I have it running, but when I post an Envelope I get an error stating my namespace is incorrect.

How to I set up my .net Core 6 API Service to expect my desired namespace?

Here is what I have:

My Model:

[DataContract(Name = "TestCustomModel", Namespace = "https://myNamespace.com")]
    public class MyCustomModel
    {
        [DataMember]
        public int username { get; set; }

        [DataMember]
        public string password { get; set; }

        [DataMember]
        public int orgId { get; set; }
        
        [DataMember]
        public int poolId { get; set; }

        [DataMember]
        public string cycleStartDate { get; set; }
    }

My Service:

 [ServiceContract(Namespace = "https://myNamespace.com")]
    public class SampleService : ISampleService
    {

        
        public string Test(string s)
        {
            Console.WriteLine("Test Method Executed!");
            return s;
        }

        public void XmlMethod(XElement xml)
        {
            Console.WriteLine(xml.ToString());
        }

        public MyCustomModel TestCustomModel(MyCustomModel customModel)
        {
            return customModel;
        }

    }

My Service interface:

[ServiceContract]
    public interface ISampleService
    {
        [OperationContract]
        string Test(string s);

        [OperationContract]
        void XmlMethod(System.Xml.Linq.XElement xml);

        [OperationContract]
        MyCustomModel TestCustomModel(MyCustomModel inputModel);
    }

Here is the SOAP I send:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="https://myNamespace.com">
    <soapenv:Header/>
    <soapenv:Body>
        <ns:TestCustomModel>
            <userName>USERNAME</userName>
            <password>PASSWORD</password>
            <orgId>123</orgId>
            <poolId>1234</poolId>
            <cycleStartDate>2023-01-01</cycleStartDate>
        </ns:TestCustomModel>
    </soapenv:Body>
</soapenv:Envelope>

But I get this error: enter image description here

Based on what I could find, I believe I am setting the namespace in the correct locations, but it still expects tempuri.org


Solution

  • Turns out I needed to move the namespace declaration from my Service to its interface.

    My service:

       public class SampleService : ISampleService
        {
            
            public string Test(string s)
            {
                Console.WriteLine("Test Method Executed!");
                return s;
            }
    
            public void XmlMethod(XElement xml)
            {
                Console.WriteLine(xml.ToString());
            }
    
          
            public MyCustomModel TestCustomModel(MyCustomModel TestCustomModel)
            {
                return TestCustomModel;
            }
    
        }
    

    My Service interface:

    [ServiceContract(Namespace = "https://myNamespace.com")]
        public interface ISampleService
        {
            [OperationContract]
            string Test(string s);
    
            [OperationContract]
            void XmlMethod(System.Xml.Linq.XElement xml);
    
            [OperationContract]
            MyCustomModel TestCustomModel(MyCustomModel inputModel);
        }