Sending a complex type as a parameter in a SOAP message

I have a WCF service, for example:

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 call like this:

  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 a complex type of type Person as a parameter to the GetData method, the person object comes up null. But I have no problem when I send as a parameter a known type of type integer, string, etc.

How do I pass a complex type as a parameter to a service method?

+4
source share
1 answer

I ran into a similar situation, and we ended up changing the service interface as an equivalent:

 public string GetData(string person) 

And we did our serialization of objects before calling the web service. Immediately as part of the web service method, we will deserialize it and act as usual.

+1
source

Source: https://habr.com/ru/post/1443454/


All Articles