RESTful WCF json请求和json响应返回null

问题描述 投票:2回答:4

我正在尝试为RESTful WCF构建一个示例。请求和响应是JSON。我得到的回应是:

{"FirstName":null,"LastName":null}

我需要得到适当的回应。

这是代码:

Web.config具有Restful的配置:

服务合约:

[OperationContract]
        [WebInvoke(UriTemplate = "Data", 
            ResponseFormat = WebMessageFormat.Json)]
        person getData(person name);

执行:

public person getData(person name)
{
    return new person{ FirstName= name.FirstName, LastName= name.LastName };
}

[DataContract]

public class person 
{

[DataMember]
public string FirstName;

[DataMember]
public string LastName;
}

客户:

class Program
{
static void Main(string[] args)
{
            string baseAddress = "http://localhost/RESTfulService";
 SendRequest(baseAddress + "/Data", "POST", "application/json", @"{""getData"" : {""name"" :{""FirstName"":""John"", ""LastName"":""Doe""}}");
}

  public static string SendRequest(string uri, string method, string contentType, string body)

    {
        string responseBody = null;

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.Method = method;
        if (!String.IsNullOrEmpty(contentType))
        {
            req.ContentType = contentType;
        }
        if (body != null)
        {
            byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
            req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
            req.GetRequestStream().Close();
        }

        HttpWebResponse resp;
        try
        {
            resp = (HttpWebResponse)req.GetResponse();
        }
        catch (WebException e)
        {
            resp = (HttpWebResponse)e.Response;
        }
        Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
        foreach (string headerName in resp.Headers.AllKeys)
        {
            Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
        }
        Console.WriteLine();
        Stream respStream = resp.GetResponseStream();
        if (respStream != null)
        {
            responseBody = new StreamReader(respStream).ReadToEnd();
            Console.WriteLine(responseBody);
        }
        else
        {
            Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
        }
        Console.WriteLine();
        Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
        Console.WriteLine();

        return responseBody;
    }

}

c# .net wcf json wcf-rest
4个回答
1
投票
public static string SendRequest(string uri, string method, string contentType, string body) {...}

不知何故,它只适用于“GET”方法。

对于POST方法,我不得不在客户端以不同的方式调用服务操作:

uri = "http://localhost/RestfulService";

EndpointAddress address = new EndpointAddress(uri);
WebHttpBinding binding = new WebHttpBinding();
WebChannelFactory<IRestfulServices> factory = new WebChannelFactory<IRestfulServices>(binding, new Uri(uri));
IRestfulServices channel = factory.CreateChannel(address, new Uri(uri));

channel.getData(new Person{firstName = 'John', LastName = 'Doe'});

[ServiceContract]
public interface IRestfulService
{
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "Data")]
    object getData(Person name)
}

0
投票

我相信DataContract对象需要一个无参数构造函数,以便序列化程序正常工作。即public Person(){},您可能还需要为公共成员添加getter和setter,public string FirstName {get;组; }。


0
投票

问题:

获取{}的空JSON响应

enter image description here

界面:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "Client/{idsHashed}", ResponseFormat = WebMessageFormat.Json)]
Summary GetClientDataById(string idsHashed);

网络方法:

public Summary GetClientDataById(string idsHashed)
{
    Summary clientSum = new Summary().GetClientDataById(clientId);
    return clientSum;
}

在课堂上,我把田地变成了内部和私人!

public class Summary
{
    private string clientName { get; set; }  //<--  wont be rendered out as json because its private

解:

检查类属性是否为Public。


0
投票

1.宁静的wcf不需要方法名称。 2.Json反序列化器不需要参数名称,它将其视为属性名称。所以使用{""name"" :{""FirstName"":""John"", ""LastName"":""Doe""}"而不是{""getData"" : {""name"" :{""FirstName"":""John"", ""LastName"":""Doe""}}

© www.soinside.com 2019 - 2024. All rights reserved.