How do I overload method parameter using NET5 SoapCore

310 Views Asked by At

I'm trying to port an existing Soap WebService to .NET5 but am having issue with overloading a Soap method parameter.

In NET4 the code looks like this

namespace SoapWebServiceeTest.Soap
{
    /// <summary>
    /// Summary description for WsgSPServiceOrderService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class TextWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string Test(
            [XmlElement("object1", typeof(Object1))]
            [XmlElement("object2", typeof(Object2))]
            [XmlElement("object3", typeof(Object3))]
            object request)
        {
            return $"{request.GetType().Name}";
        }
    }

    public class Object1 { public string Param1 { get; set; } }
    public class Object2 { public string Param2 { get; set; } }
    public class Object3 { public string Param3 { get; set; } }
}

How do I achieve this in .NET5?

I have tried following but got reflection exception: System.Reflection.AmbiguousMatchException: 'Multiple custom attributes of the same type found.'

[ServiceContract]
public interface ITestWebService
{
    [OperationContract]
    string Test(
        [XmlElement("object1", typeof(Object1))]
        [XmlElement("object2", typeof(Object2))]
        [XmlElement("object3", typeof(Object3))]
        object request);
}

And also tried this but VS Add Connected Services errored with "More than one message named 'ISampleService_Test_InputMessage' was specified. Each message must have a unique name."

[OperationContract]
string Test(Object1 request);

[OperationContract]
string Test(Object2 request);

Any help would be awesome

1

There are 1 best solutions below

1
Zahid Mustafa On

You may try this

You can make it post or get based on your need

[OperationContract]
[WebInvoke(UriTemplate = "Test1", Method = "POST"]
string Test(Object1 request);

and

[OperationContract]
[WebInvoke(UriTemplate = "Test2", Method = "POST"]
string Test(Object2 request);

This way you can achieve objective