[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
[WebInvoke(UriTemplate = "",
Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare) ]
public SomeObject DoPost(string someText)
{
...
return someObject;
在小提琴手中,我的请求标题和正文会是什么样的?
感谢您的帮助。
答案 0 :(得分:16)
我稍微改变了你的WCF服务,以便有一个更好的例子并编写一个示例测试程序(见下文)。
第一个测试执行URL http:// localhost:57211 / Service1.svc / getcar / 1 的GET请求。最后的 1 是一个参数。在您的情况下,端口号可能会有所不同。结果是:
{"ID":1,"Make":"Porsche"}
第二个测试通过将相同的数据(保时捷的法拉利除外)发送到URL http:// localhost:57211 / Service1.svc / updatecar / 1 来执行POST请求。结果是:
{"ID":1,"Make":"Ferrari"}
此请求在URL中有一个参数( 1 )加上请求数据(JSON结构)作为第二个参数,作为请求主体传输。
使用网络调试器,它看起来像这样(简化):
POST /Service1.svc/updatecar/1 HTTP/1.1
Host: localhost:57211
Content-Type: application/json
Content-Length: 25
{"ID":1,"Make":"Ferrari"}
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 25 Dec 2010 19:16:19 GMT
Content-Length: 25
Content-Type: application/json; charset=utf-8
{"ID":1,"Make":"Ferrari"}
我希望有所帮助。
<强> TestService.cs:强>
class TestService
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:57211/Service1.svc/getcar/1");
WebResponse response = request.GetResponse();
string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(result);
string requestData = "{\"ID\":1,\"Make\":\"Ferrari\"}";
byte[] data = Encoding.UTF8.GetBytes(requestData);
request = (HttpWebRequest)WebRequest.Create("http://localhost:57211/Service1.svc/updatecar/1");
request.Method = "POST";
request.ContentType = "application/json";
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
response = request.GetResponse();
result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(result);
}
}
<强> IService.cs:强>
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/getcar/{id}")]
Car GetCar(string id);
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/updatecar/{id}")]
Car UpdateCar(string id, Car car);
}
[DataContract]
public class Car
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Make { get; set; }
}
<强> Service.svc:强>
public class Service1 : IService1
{
public Car GetCar(string id)
{
return new Car { ID = int.Parse(id), Make = "Porsche" };
}
public Car UpdateCar(string f, Car car)
{
return car;
}
}
Service1.svc(加价):
<%@ ServiceHost Language="C#" Debug="true" Service="JSONService.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
答案 1 :(得分:1)
json只是一个字符串,可以很容易地被ajax或java脚本使用。对于像c#这样的其他语言,只需编写一个简单的逻辑就可以将此字符串强制转换为自定义类。默认情况下,将数据保存为jcf deserilizad将数据转换为json
答案 2 :(得分:1)
DataContractJsonSerializer serializer = new DataContractJsonSerializer(MyClass.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, MyClass);
string JsonString = Encoding.Default.GetString(ms.ToArray());
运行上面的代码,将了解JSON的外观......其中MyClass
是任何类的对象
答案 3 :(得分:0)
我猜你丢失的那条信息是WCF来回传递序列化对象。问题是,JSON序列化字符串对象是什么样的。从未在WCF中使用JSON我无法告诉你。
但是,我建议如果您尝试传递someObject2而不是someText,那么您可能会获得更多成功。