RestSharp将对象发布到WCF [英] RestSharp post object to WCF

查看:99
本文介绍了RestSharp将对象发布到WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将对象发布到WCF REST Web服务时遇到问题.

在WCF方面,我有以下内容:

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

调试时,断点不会被击中,但是删除参数时断点会被击中,所以我猜我在RestSharp方面做错了事.

这是我那部分的代码:

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

request.RequestFormat = DataFormat.Xml;        

request.AddBody(myObject);

var response = client.Execute(request);

我做错了吗? WCF一方如何看到我的对象?我应该以哪种方式提出请求?还是我应该在WCF方面以不同的方式处理它?<​​/p>

我尝试过的事情:

request.AddObject(myObject);

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

任何可能出错的帮助和理解将不胜感激.谢谢.

解决方案

我一直在努力解决同样的问题.一旦尝试添加要传递的对象,它就会变成错误请求".我根据发现的各个站点尝试了各种方法,但一无所获.然后,我将格式从Xml更改为Json,它才开始起作用. XML传递一定存在故障.可能需要设置第二台PC并尝试使用Wireshark或Fiddler之类的东西嗅探实际的http. (或者也许我会坚持使用json)

下面是我的实验性WCF界面中的功能

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

然后测试我的RestSharp客户端

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

也许有人可以对此事提供更多的启示.我也是REST服务的新手.我以为我会添加自己的发现,以寻求更好的答案.

(-EDIT-) 我进行了一些进一步的挖掘,发现了此花絮 因此,我像这样向ServiceContract接口添加了[XmlSerializerFormat]属性

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

然后这终于奏效了,我在服务中得到了一个对象

            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-)我遇到了更多XML序列化异常,这使我进行了扩展(从此处还有一个答案,表明您需要使用公共属性正确序列化,而我还没有尝试过.

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();
    }

}

所以我的RestSharp呼叫开始看起来像这样

    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>();
    }

这个答案越来越长,但我希望它对某人有所帮助.

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.

解决方案

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.

这篇关于RestSharp将对象发布到WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆