如何通过JSON请求WCF RESTful服务调用的提琴手? [英] how to call wcf restful service from fiddler by JSON request?

查看:199
本文介绍了如何通过JSON请求WCF RESTful服务调用的提琴手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WCF RESTful服务新。我找不到这是为什么我的WCF RESTful服务给错误的请求的问题。我使用.NET 4.0

I am new in wcf restful service. I couldn’t find the problem which was, why my wcf restful service give ‘bad request’. I use .NET 4.0.

我的服务是:

[OperationContract(Name="Add")]
[WebInvoke(UriTemplate = "test/", Method = "POST",
          ResponseFormat=WebMessageFormat.Json,
          RequestFormat=WebMessageFormat.Json )]
public int Add(Number n1)
{
    res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
    return res;
}

数据是..

[Serializable]
    public class Number
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }

当我从小提琴手它的返回称之为HTTP / 1.1 400错误的请求

When I call from fiddler its return ‘HTTP/1.1 400 Bad Request’

我的提琴手请求头为:

User-Agent: Fiddler
Host: localhost:4217
Content-Type: application/json; charset=utf-8

并请求体是:

{"Number1":"7","Number2":"7"}

并响应头为:

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Sun, 14 Aug 2011 18:10:21 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 5450
Cache-Control: private
Content-Type: text/html
Connection: Close

但是,如果我用C#的客户端程序,这是确定调用这个服务。

But if I call this service by C# client program it is ok.

我的客户端代码:

uri = "http://localhost:4217/Service1.svc/";
Number obj = new Number() { Number1 = 7, Number2 = 7 };
using (HttpResponseMessage response = new HttpClient().Post(uri+"test/",
       HttpContentExtensions.CreateDataContract(obj)))
{
    string res = response.Content.ReadAsString();
    return res.ToString();
}

请帮我........

please help me........

感谢。

推荐答案

你会发现你的代码这个问题的方法是的enable跟踪中的服务器上,并且它会产生一个异常解释问题。我写了一个简单的测试与你的代码,它有一个类似的错误(400),和痕迹有以下错误:

The way you'll find out the issue with your code is to enable tracing on the server, and it will have an exception explaining the problem. I wrote a simple test with your code, and it had a similar error (400), and the traces had the following error:

The data contract type 'WcfForums.StackOverflow_7058942+Number' cannot be
deserialized because the required data members '<Number1>k__BackingField,
<Number2>k__BackingField' were not found.



标记中的数据类型[Serializable接口] 连载所有的字段的对象,而不是性能。通过注释掉该属性,代码实际工作了罚款(那么该类型属于POCO(普通的旧CLR对象)规则,序列化的所有公共字段和属性。

Data types marked with [Serializable] serialize all the fields in the object, not properties. By commenting out that attribute, the code actually works out fine (the type then falls into the "POCO" (Plain Old Clr Object) rule, which serializes all public fields and properties.

public class StackOverflow_7058942
{
    //[Serializable]
    public class Number
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }
    [ServiceContract]
    public class Service
    {
        [OperationContract(Name = "Add")]
        [WebInvoke(UriTemplate = "test/", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public int Add(Number n1)
        {
            int res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
            return res;
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        c.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
        c.Encoding = Encoding.UTF8;
        Console.WriteLine(c.UploadString(baseAddress + "/test/", "{\"Number1\":\"7\",\"Number2\":\"7\"}"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

这篇关于如何通过JSON请求WCF RESTful服务调用的提琴手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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