在vs2013下使用wcfRestservice40 是安装不成功的,尝试多遍,都是这样。查看以前vs2012做的wcfrest,经过调教,终于在vs2013下也可以了!
1.新建wcf服务应用程序
2.调制配置文件
<?xml version="1.0"?><configuration> <system.web> <compilation debug="true" targetFramework="4.0"/> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </modules> </system.webServer> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <standardEndpoints> <webHttpEndpoint> <!-- Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the <standardEndpoint> element below --> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> </webHttpEndpoint> </standardEndpoints> </system.serviceModel></configuration>View Code
3.添加全局应用程序类 Global.asax
里面代码:
void application_Start(object sender, EventArgs e) { RegisterRoutes(); } PRivate void RegisterRoutes() { RouteTable.Routes.Add( new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1))); //RouteTable.Routes.Add("test", new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1))); }View Code
4.新建Service1.cs类库 (以后作为服务的节点)
[ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class Service1 { [WebGet(UriTemplate = "")] public List<SampleItem> GetCollection() { return new List<SampleItem>(){new SampleItem(){Id = 1,StringValue = "Hello"}}; } } public class SampleItem { public int Id { get; set; } public string StringValue { get; set; } }Service1
5.运行调试,
刚运行的时候
运行服务,在当前地址后面添加上面的服务节点
可以再路径后面添加“help/”查看服务的各个方法。
到此结束!
新闻热点
疑难解答