RestSharp post object to WCF

走远了吗. 提交于 2019-12-11 06:38:34

问题


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.


回答1:


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.




回答2:


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");


来源:https://stackoverflow.com/questions/9966521/restsharp-post-object-to-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!