tags:

views:

103

answers:

1

I have a wcf restful service with a operation contract that contains two values - an int and a string. This is a post call as well.

If i wrap the call using the BodyStyle = WebMessageBodyStyle.Wrapped. What should i assume the xml request will now look like?

A: 

Hello -- I ran into the same problem and I believe there are a couple of ways that you can consider doing this. If there are more I'd love to hear them from others.

First, I'll tell you how I accomplished this. Step one is to build a utility library that contains a class definition for your data object structure, with appropriate get methods and constructor to initilize the object. Also, you must make the class serializable i.e.

[Serializable]
public class myDataObject
{
    private int _n1;
    private string _s1;

    public myDataObject(int n, string s)
    {
        this._n1 = n;
        this._s1 = s;
    }

    public int getN1()
    {
        return this._n1;
    }
    public string getS1()
    {
        return this._s1;
    }
}

}

It is important to put this in a library so you can reference it from both the client and server side.

Once you've done that, change your WCF service method to look similar to the following:

[WebInvoke(Method = "POST", UriTemplate = "yourDesignedURI")]
[OperationContract]
public bool doSomething(myDataObject o)
{ 
    //implement your service logic, accessing the parameters from o
    int i = o.getN1();
    string s = o.getS1();
    //...etc
    return true;
}

Once this is complete, publish your service, and then use the help page from your service to view the XML syntax that is required. You can then build your XML using Linq to XML and thereby send your data object over the web as a request. This eliminates the need to wrap and exposes your request xml syntax:

<myDataObject xmlns="http://schemas.datacontract.org/2004/07/DemoXMLSerialization"&gt;
    <_n1>2147483647</_n1>
    <_s1>String content</_s1>
</myDataObject>

Reference the utility library in your client and create a method in your data later to call the service method, creating the element in your method.

public void callService(int n1, string s1)
    {
        myDataObject o = new myDataObject(n1, s1);
        string serviceURL = "yourBaseURL";
        string serviceURI = "yourDesignedURI";

        using (HttpClient client = new HttpClient(serviceURL))
        {
            client.DefaultHeaders.Add("Content-Type", "application/xml; charset=utf-8");

            XNamespace xns = "http://schemas.datacontract.org/2004/07/DemoXMLSerialization";
            XDocument xdoc = new XDocument(
                                new XElement(xns + "myDataObject",
                                new XElement(xns + "_n1", o.getN1())
                                , new XElement(xns + "_s1", o.getS1())));
            using (HttpResponseMessage res = client.Post(serviceURI, HttpContent.Create(xdoc.ToString(SaveOptions.DisableFormatting))))
            {
                res.EnsureStatusIsSuccessful();
                //do anything else you want to get the response value
            }
        }
    }

The other thing you can do is redesign your uri to be something like this: yourdesignedURI/{myInt}/{myString}

and then use JSON Serialization to send the object. If you add the line

BodyStyle=WebMessageBodyStyle.Bare

To your WebInvoke attribute, the help page will expose the full URI for easy viewing.

I am sure there is probably a way to do it with JQuery as well. Would love to see other solutions to this!

Hope that helps.

COMCY2002