Hey, super newbie questi开发者_如何转开发on. Consider the following WCF function:
[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;
In fiddler what would my request headers and body look like?
Thanks for the help.
I've slightly changed your WCF service to have a better example and written a sample test program (see below).
The first test executes a GET request for the URL http://localhost:57211/Service1.svc/getcar/1. The 1 at the end is a parameter. The port number may be different in your case. The result is:
{"ID":1,"Make":"Porsche"}
The second test executes a POST request by sending the same data (except Ferrari for Porsche) to the URL http://localhost:57211/Service1.svc/updatecar/1. The result is:
{"ID":1,"Make":"Ferrari"}
This request has both a parameter in the URL (the 1) plus the request data (a JSON structure) as a second parameter, transmitted as the request body.
With a network debugger, it would look like this (simplified):
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"}
I hope that helps.
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 (Markup):
<%@ ServiceHost Language="C#" Debug="true" Service="JSONService.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
json is nothing but a string which can be easily used by ajax or java script. for other languages like c# u can custimize this string into custom class by just writing a simple logic.and by default restfull wcf deserilizad the data into json
DataContractJsonSerializer serializer = new DataContractJsonSerializer(MyClass.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, MyClass);
string JsonString = Encoding.Default.GetString(ms.ToArray());
Run the above code, will get to know how the JSON looks like... where MyClass
is the object of any class
I would guess that the piece of information that you are missing is that WCF passes back and forth serialized objects. The question is, what does a JSON serialized string object look like. Having never used JSON with WCF I can't tell you.
However, I can suggest if you tried passing someObject2 instead of someText you will probably have more success.
精彩评论