C# RESTful WCF json请求和json响应返回null
我正在尝试为RESTful WCF构建一个示例。请求和响应是JSON。我得到的答复是:C# RESTful WCF json请求和json响应返回null,c#,.net,wcf,json,wcf-rest,C#,.net,Wcf,Json,Wcf Rest,我正在尝试为RESTful WCF构建一个示例。请求和响应是JSON。我得到的答复是: {"FirstName":null,"LastName":null} 我需要得到适当的回应 代码如下: Web.config具有Restful的配置: 服务合同: [OperationContract] [WebInvoke(UriTemplate = "Data", ResponseFormat = WebMessageFormat.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;
}
}我认为DataContract对象需要一个无参数构造函数,序列化程序才能正常工作。例如,public Person(){},您可能还需要为公共成员、公共字符串FirstName{get;set;}添加getter和setter
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)
}
uri=”http://localhost/RestfulService";
EndpointAddress地址=新的EndpointAddress(uri);
WebHttpBinding=新的WebHttpBinding();
WebChannelFactory=newWebChannelFactory(绑定,新Uri(Uri));
IRestfulServices通道=工厂.CreateChannel(地址,新Uri(Uri));
getData(新人{firstName='John',LastName='Doe'});
[服务合同]
公共接口IRestfulService
{
[WebInvoke(BodyStyle=WebMessageBodyStyle.WrappedRequest,UriTemplate=“Data”)]
对象getData(人名)
}
问题:
获取空的JSON响应{}
界面:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "Client/{idsHashed}", ResponseFormat = WebMessageFormat.Json)]
Summary GetClientDataById(string idsHashed);
web方法:
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
公共类摘要
{
私有字符串clientName{get;set;}/1。restful wcf不需要方法名。
2.Json反序列化程序不需要参数名,它将其视为属性名。
因此,请使用{“name”:{“FirstName”:“John”,“LastName”:“Doe”}“
而不是
{“getData”“:{“name”“:{“FirstName”“:”“John”“LastName”“:”“Doe”“}}
返回新的mytype是什么意思?mytype是什么意思?
?为正确的代码编辑的..这只是一个旁注:您使用的是包装消息,即方法名(getData)也是消息的一部分。这真的是RESTful服务还是只是JSON伪装的SOAP?如果您发现使用裸消息要优雅得多:([WebInvoke(UriTemplate=“Data/getData”,BodyStyle=WebMessageBodyStyle.bare,ResponseFormat=WebMessageFormat.JSON)]。这也可能是您的问题的一部分,因为我怀疑您的消息与WCF的预期不符。您是正确的,我需要将此服务用作SOAP和RESTful。是否可以实现此功能,我使用bare进行了尝试,但仍然不起作用