WCF Rest Json

时间:2023-03-09 15:55:38
WCF Rest Json

1、定义ServiceContract及实现

    [ServiceContract]
public interface IMemberService
{
[OperationContract]
string GetData(int value); [OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite); [OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/create", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
string CreateMember(Member member); [OperationContract]
[WebGet(UriTemplate = "all")]
string GetAll();
// TODO: 在此添加您的服务操作
}

2、svc文件

<%@ServiceHost language=c# Debug="true" Service="WebApplication1.MemberService" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

3、Fiddler测试

Request Headers:

User-Agent: Fiddler
Content-Type: application/json;charset=UTF-
Host: localhost:
Content-Length: RequestBody:
{"member":{"ID":"Brett","Name":"McLaughlin"}}