1

I am having an issue posting an object to my WCF REST Web Service.

On the WCF side I have the following:

[WebInvoke(UriTemplate = "", Method = "POST")]
public void Create(myObject object)
{
//save some stuff to the db
}

When I am debugging, the break point is never hit.However, the break point is hit when I remove the parameter.So, I am guessing I have done something wrong on the RestSharp side of things.

Here's my code for that part:

var client = new RestClient(ApiBaseUri);
var request = new RestRequest(Method.POST);       

request.RequestFormat = DataFormat.Xml;        

request.AddBody(myObject);

var response = client.Execute(request);

Am I doing this wrong? How can the WCF side see my object? What way should I be making the request? Or should I be handling it differently on the WCF side?

Things that I have tried:

request.AddObject(myObject);

and

request.AddBody(request.XmlSerialise.serialise(myObject));

Any help and understanding in what could possibly be wrong would be much appreciated. Thanks.

Magellan
  • 71
  • 1
  • 2
  • 11
steve
  • 128
  • 1
  • 4
  • 10

2 Answers2

3

I have been struggling with the same problem. Once you try to add the object to pass, it becomes a "Bad request". I tried a variety of things based on various sites I found and got nothing. Then I changed the format from Xml to Json, and it just started working. Must be some glitch with XML passing. Might need to setup a 2nd PC and try to sniff the actual http with something like wireshark or fiddler. (Or maybe I'll just stick to json)

Below is the function from my experimental WCF interface

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "manualselect", ResponseFormat = WebMessageFormat.Json)]
    void PostManualSelect(ManualUpdateRequest S);

then my test RestSharp client

            var client = new RestClient();
        client.BaseUrl = "http://127.0.0.1:8000";
        /* Initialization of ManualUpdateRequest instance "DR" here */

        var request = new RestRequest(Method.POST);
        request.Resource = "manualselect";
        request.RequestFormat = DataFormat.Json;
        request.AddBody(DR);
        RestResponse response = client.Execute(request);

Perhaps someone can shed some more light on the matter. I am also new to REST services. I'd thought I'd add my findings to steer towards a better answer.

(--EDIT--) I did some more digging and found this tidbit So I added the [XmlSerializerFormat] attribute to ServiceContract interface like so

[ServiceContract]
[XmlSerializerFormat]
public interface IMyRestService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "manualselect", ResponseFormat = WebMessageFormat.Xml)]
    void PostManualSelect(ManualUpdateRequest S);
}

and then this finally worked and I got an object in my service

            var client = new RestClient();
        client.BaseUrl = "http://127.0.0.1:8000";
        /* Initialization of ManualUpdateRequest instance "DR" here */

        var request = new RestRequest(Method.POST);
        request.Resource = "manualselect";
        request.RequestFormat = DataFormat.Xml;
        request.AddBody(DR);
        RestResponse response = client.Execute(request);

(--EDIT 2--) I have encountered some more XML serializing weirdness that lead me to make this extension (borrowing from here). Might help if you still have trouble. There is also an answer here that implies you need to use public properties to serialize correctly, which I have not tried yet.

public static class RestSharpExtensions
{
    public static T GetXmlObject<T>(this IRestResponse response)
    {
        if (string.IsNullOrEmpty(response.Content))
        {
            return default(T);
        }

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        XmlReaderSettings settings = new XmlReaderSettings();
        // No settings need modifying here

        using (StringReader textReader = new StringReader(response.Content))
        {
            using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
            {
                return (T)serializer.Deserialize(xmlReader);
            }
        }
    }

    public static void UseDotNetXml(this IRestRequest request)
    {
        request.RequestFormat = DataFormat.Xml;
        request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
    }

}

So my RestSharp calls start looking more like this

    public SimpleSignUpdateDataSet GetSimpleDataset()
    {
        var client = new RestClient(SerivceURL);

        var request = new RestRequest("simpledataset", Method.GET);
        request.UseDotNetXml();

        var resp = client.Execute(request);
        return resp.GetXmlObject<SimpleSignUpdateDataSet>();
    }

This answer is getting long, but I hope it is of some help to someone.

Community
  • 1
  • 1
CFD
  • 281
  • 2
  • 10
  • thanks for taking the time to reply - tried your suggestions - im afraid im still getting "bad request" response. Hopefully it can help someone else out – steve Apr 21 '12 at 09:48
  • Thanks for this, you saved me hours of work. My issue was solved by adding [XmlSerializerFormat] – paulio Aug 20 '12 at 10:59
-1

you can use fiddler on the same pc .... no need for a second one. If you install it, solving these types of problems gets really much easier, you see what you do!

Specify proxy like this:

using system.net; // for the WebProxy

RestClient rc = new RestClient(aUrl);
rc.Proxy = new WebProxy("http://127.0.0.1:8888");
Paul0515
  • 23,515
  • 9
  • 32
  • 47