WCF REST服务开发

1、新建项目,打开VS2010,WCF/WCF服务库

 

2、确保引用System.ServiceModel.Web,.net 框架不要选择client profile

 

3、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>

4、IService1.cs

[ServiceContract]
public interface IService1
{
[OperationContract]
Person GetData(string id);
}

 

5、Service1.cs

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; }
}

6、F5执行,在浏览器输入

http://localhost:8732/Service1/data/10

{"Id":10,"Name":"Leo Messi"}
posted @ 2013-06-07 16:25  羽化成蝶  阅读(190)  评论(0编辑  收藏  举报