class Program { static void Main(string[] args) { var a = JsonConvert.SerializeObject(new { b = 1999 }); var r = HttpHelper.PostRequest("http://localhost:5829/Service1.svc/GetData", DataTypeEnum.Json, a); Console.WriteLine(r); Console.ReadKey(); } }
[ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemplate = "GetData", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] string GetData(MyClass b); }
public class Service1 : IService1 { public string GetData(MyClass b) { return string.Format("You entered: {0}", b.b); } } public class MyClass { public int b { get; set; } }
web.config添加配置web
<service name="WcfService1.Service1"> <endpoint address ="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="web" > </endpoint> </service> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors>
返回結果ide