我正在尝试为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;
}
}
答案 0 :(得分: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)
}
答案 1 :(得分:0)
我相信DataContract对象需要一个无参数构造函数才能使序列化程序正常工作。即public Person(){},您可能还需要为公共成员添加getter和setter,public string FirstName {get;组; }。
答案 2 :(得分:0)
<强>问题:强>
获取 {}
的空JSON响应界面:
[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
<强>解决方案:强>
检查类属性是否公开。