Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending complex type as a parameter in SOAP message

I have a WCF Service like following:

 public class Service1 : IService1
{
    public string GetData(Person person)
    {
        if (person != null)
        {
            return "OK";
        }
        return "Not OK!";
    }

Here is my Person class:

 [DataContract]
public class Person
{
    [DataMember]
    public int Age { get; set; }

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

And I'm calling service like that:

 BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
        IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
        factory.Open();

        EndpointAddress address = new EndpointAddress(url);
        IRequestChannel irc = factory.CreateChannel(address);
        using (irc as IDisposable)
        {
            irc.Open();
            string soapMessage = "<GetData><person><Age>24</Age><Name>John</Name></person></GetData>";

            XmlReader reader = XmlReader.Create(new StringReader(soapMessage));
            Message m = Message.CreateMessage(MessageVersion.Soap11,"http://tempuri.org/IService1/GetData", reader);

            Message ret = irc.Request(m);
            reader.Close();
            return ret.ToString();
        }

When I try to send complex type like Person as a parameter to GetData method, person object is coming null. But I have no problem when I send known type like integer, string etc. as a parameter.

How can I manage to send complex type as a parameter to the service method?

like image 844
Abdurrahman Alp Köken Avatar asked Nov 12 '22 18:11

Abdurrahman Alp Köken


1 Answers

I ran into a similar situation, and we ended up changing the interface of the service to be the equivalent of:

public string GetData(string person)

And we did our own object serialization before calling the web service. Immediately within the web service method we would deserialize it, and proceed as normal.

like image 94
Andrew Lewis Avatar answered Dec 19 '22 08:12

Andrew Lewis