如何通过JSON对象在WCF休息吗? [英] How to pass Json object in WCF Rest?

查看:119
本文介绍了如何通过JSON对象在WCF休息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WCF休息40(CS)模板我REST项目结果
我传递的数据通过URL时,并没有得到任何的问题:

I use WCF Rest 40(CS) Template for my REST project.
I didn't get any problem when passing a data through URL:

http://localhost:8525/Device/Login?deviceID=testid&password=a&serialNum=testserial  

但传递JSON数据时,我得到:

But when passing Json data, I get:

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 17 Feb 2012 08:41:38 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 1647
Set-Cookie: ASP.NET_SessionId=4tez22jvuy0s3tiioctukvdq; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html
Connection: Close

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. See server logs for more details.</p>
    </div>
  </body>
</html>  



不是允许的方法结果
(使用招响应)

Method not Allowed
(Response using Fiddler)

在端服务:

namespace WCFRest.Rest
{
    [ServiceContract]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DeviceComponent
    {
        [WebInvoke(UriTemplate = "Login?deviceID={deviceID}&password={password}&serialNum={serialNum}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public WRModel.Entities.Session Login(string deviceID, string password, string serialNum)
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            WRModel.Interfaces.IDeviceComponent svc = WRModel.Logic.ConfigAndResourceComponent.Containers().Resolve<WRModel.Interfaces.IDeviceComponent>();
            return svc.Login(deviceID, password, serialNum, ip);
        }

        [WebInvoke(UriTemplate = "Logout", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public bool Logout(WRModel.Entities.DeviceSession deviceSession)
        {
            WRModel.Interfaces.IDeviceComponent svc = WRModel.Logic.ConfigAndResourceComponent.Containers().Resolve<WRModel.Interfaces.IDeviceComponent>();
            return svc.Logout(deviceSession);
        }
    }
}  



登录部分工作正常,但在注销会导致错误。我哪里做错了?感谢帮助。这里是一些图像使用招:

The Login part works fine, but the Logout causes error. Where did I go wrong? Thanks for help. Here some image using fiddler:

修改

DeviceSession实体

DeviceSession Entity

[DataContract(Name = "DeviceSession", Namespace = "http://MySite.com/1.0/DeviceSession")]
public class DeviceSession : IDeviceSession
{
public DeviceSession()
{
}

public DeviceSession(string session, string loginID, string serial)
{
    Session = session;
    LoginID = loginID;
    Serial = serial;
}

string _session;

[DataMember(Order = 0)]
public string Session
{
    get { return _session; }
    set { _session = value; }
}

string _LoginID;

[DataMember(Order = 1)]
public string LoginID
{
    get { return _LoginID; }
    set { _LoginID = value; }
}

string _serial;

[DataMember(Order = 2)]
public string Serial
{
    get { return _serial; }
    set { _serial = value; }
}



}

}

推荐答案

您实际上在每个例子做了稍微不同的事情。在第一个例子(登录),你在URL中张贴变量,你体内张贴的第二位。

You have actually done slightly different things in each example. In the first example (login) you are posting variables on the url, in the second you are posting in the body.

请告诉我需要注意的是,JSON你需要在第二个例子中后期是字符串的JSON,而不是JSON包含一个字符串的对象。这意味着JSON心不是有效的类型字符串。

Whats important to note is that the JSON you need to post in the second example is the json for string, not the json for an object containing a string. This means that the json isnt valid for the type string.

我会建议是包装在一个对象或调用它的查询字符串作为你的第一个例子

What i would recommend is to wrap it in an object or call it on the query string as in your first example.

例如,你可以使用

 [WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
 public WRModel.Entities.Session Login(LoginDetails details)

在哪里登录信息是包含DEVICEID密码的serialNumber类

Where login details is a class containing deviceId Password serialNumber

这篇关于如何通过JSON对象在WCF休息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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