60秒创建JSON WCF RESTful服务
2011-03-15 23:53
232 查看
1、先配置App.Config文件:
<?xml version="1.0"?>
<configuration> <system.serviceModel> <services> <service name="WcfJsonRestService.Service1"> <endpoint address="http://localhost:8732/service1" binding="webHttpBinding" contract="WcfJsonRestService.IService1"/> </service> </services> <behaviors> <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup></configuration>
2、接口定义:
using System.ServiceModel;
namespace WcfJsonRestService{ [ServiceContract] public interface IService1 { [OperationContract] Person GetData(string id); }} 3、接口实现:
using System;using System.ServiceModel.Web;
namespace WcfJsonRestService{ public class Service1 : IService1 { [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "data/{id}")] public Person GetData(string id) { // lookup person with the requested id return new Person() { Id = Convert.ToInt32(id), Name = "Leo Messi" }; } }
public class Person { public int Id { get; set; } public string Name { get; set; } }}
4、调试--启用实例,并通过浏览器访问http://localhost:8732/service1/data/10 即可看到运行效果!
<?xml version="1.0"?>
<configuration> <system.serviceModel> <services> <service name="WcfJsonRestService.Service1"> <endpoint address="http://localhost:8732/service1" binding="webHttpBinding" contract="WcfJsonRestService.IService1"/> </service> </services> <behaviors> <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup></configuration>
2、接口定义:
using System.ServiceModel;
namespace WcfJsonRestService{ [ServiceContract] public interface IService1 { [OperationContract] Person GetData(string id); }} 3、接口实现:
using System;using System.ServiceModel.Web;
namespace WcfJsonRestService{ public class Service1 : IService1 { [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "data/{id}")] public Person GetData(string id) { // lookup person with the requested id return new Person() { Id = Convert.ToInt32(id), Name = "Leo Messi" }; } }
public class Person { public int Id { get; set; } public string Name { get; set; } }}
4、调试--启用实例,并通过浏览器访问http://localhost:8732/service1/data/10 即可看到运行效果!
相关文章推荐
- WCF4.0 -- RESTful WCF Services(创建基于SSL的服务)
- WCF4.0 -- RESTful WCF Services(创建基于SSL的服务)
- 使用WCF创建Restful服务
- WCF4.0 -- RESTful WCF Services(创建基于SSL的服务)
- (转)使用WCF创建Restful服务
- jQuery调用WCF服务传递JSON对象
- 如何:使用反射提供程序创建数据服务(WCF 数据服务)
- wcf创建基于tcp协议的iis寄宿服务的第一个例子
- 调用WCF REST服务时,使用JSON
- 创建WCF服务没有SVC文件的方法
- 创建动态WCF服务(无配置文件)
- 创建寄宿在Windows服务中的WCF服务
- RestFul WCF JSON Service with client and on Mozilla Firefox –REST Client
- 用python创建一个简单的restful风格的web服务应用
- 未能创建SSL/TLS安全通道,导致的通过HTTPS协议访问WCF服务
- 5个步骤创建你的第一个RESTFul 服务
- WCF系列: 创建一个WCF服务
- 【转载】RestFul WCF JSON Service with client and on Mozilla Firefox –REST Client
- 用 Java 技术创建 RESTful Web 服务
- WCF代码创建通道,访问并执行服务方法