休息呼叫:发布对象(客户端和服务器服务端)

时间:2014-11-03 16:13:00

标签: restsharp

在发送对象时使用库RestSharp实现程序时遇到一些问题。

我实施了一个测试程序(客户端和服务器)来测试交换,我一直坚持发送一个对象:

wsDatas.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EProcess
{
    public class WsQResult
    {
        public string test { get; set; }
    }
}

IApi.cs:

using System.ServiceModel;

namespace EProcess
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        WsQResult SendHostInfos(HostInfos hostInfos);
    }
}

Api.cs:

using System;
using System.IO;
using System.ServiceModel.Web;

namespace EProcess
{
    public class Service1 : IService1
    {
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "sendHostInfos")]
        public WsQResult SendHostInfos(HostInfos hostInfos)
        {
            return new WsQResult()
            {
                test = hostInfos.userName
            };
        }
    }
}

电话:

using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EProcess
{
    public partial class wMain : Form
    {
        public wMain()
        {
            InitializeComponent();
        }

        private void btTest03_Click(object sender, EventArgs e)
        {
            //Get host infos
            HostInfos hostInfos = new HostInfos();

            //Send host infos
            var ws = new RestClient("http://localhost:8090");
            var wsQ = new RestRequest("api/sendHostInfos", Method.POST);
            wsQ.AddObject(hostInfos);

            //Send & deserialize result
            RestResponse<WsQResult> wsOut = (RestResponse<WsQResult>)ws.Execute<WsQResult>(wsQ);

            if (wsOut.Data == null)
            {
                label3.Text = "---";
            }
            else
            {
                label3.Text = wsOut.Data.test;
            }
        }

    }
}

在Api.cs&gt; public WsQResult SendHostInfos(HostInfos hostInfos), hostInfos 始终为null且wsOut.ResponseStatus出错,但我不明白为什么?

是客户端,实施不当还是服务器端(或两者)?

提前感谢您的帮助

0 个答案:

没有答案