开发者

What does the xml look like for a wrapped wcf post call?

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 BodyStyl开发者_C百科e = WebMessageBodyStyle.Wrapped. What should i assume the xml request will now look like?


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">
    <_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜