Xaisoft
Xaisoft

Reputation: 46591

How to parse soap+xml message that is passed into my wcf service operation?

I have the following Soap Request I receive from the client, where basically I have to extract the Name and then send back "Hello Test"

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"   
xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
    <ns1:Customer>
    <ns1:Name>Test</ns1:Name>
    </ns1:Customer>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

If I have a class Customer define like this:

public class Customer
{
    public string Name {get;set;}
}

I am unsure how to pass in the soap request to my wcf service operation which will take in a Customer Request Object generated from an xsd?

After my wcf service operation receives the soap request, I am not sure how to get the Name attribute out of it and send a response back to the client, like maybe "Hello Test"

Note: The client is not going to send a Customer object, they are going to send an xml request and I have to parse it into a Customer object. I hope this clears things up.

Do I have to do something like this where I pass in a XDocument to my wcf service operation:

private static void ParsSoapDocument(XDocument soapDocument)
{
   //Parse XDocument for elements/attributes

}

Upvotes: 2

Views: 2732

Answers (1)

Ethan Cabiac
Ethan Cabiac

Reputation: 4993

You should not have to parse anything, this is what WCF handles for you.

There may be variations based on whether you are using wrapped/unwrapped messages, but the basic scenario, for the soap message you describe coming from the client, your service interface would be as follows (assuming your response is a string):

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    public string Customer(string Name);
}

More likely, you are actually trying to perform an operation which takes in a Customer. For instance, to check if a customer exists you might have:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    public bool CheckCustomerExists(Customer Customer);
}

and your Customer class on the service side would need to be defined as a DataContract:

[DataContract]
public class Customer
{
    public string Name{get;set;}
}

This would make the soap request look as follows:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"> <SOAP-ENV:Body>
    <ns1:CheckCustomerExists>
    <ns1:Customer>
    <ns1:Name>Test</ns1:Name>
    </ns1:Customer>
    </ns1:CheckCustomerExists> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Upvotes: 3

Related Questions