1, 新增默认的WCF项目
用VS2017新增WCF项目, 新增项目后应该直接可以运行起来
运行起来 打开类似这个目录, 应该可以看到Service1服务的帮助文档 http://localhost:52217/Service1.svc
并可打开WebService服务: http://localhost:52217/Service1.svc?wsdl
2, 添加Restful支持: 配置Web.config
在Web.config的<system.serviceModel>节点内添加如下代码
<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 helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />
</webHttpEndpoint>
</standardEndpoints>
3, 添加Restful支持: 添加Global类
右键项目–>添加–>全局应用程序类
在Global 类中加上服务的路由
namespace WcfService1
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}
}
}
完成了
运行项目, 可以打开服务帮助页面了, 类似 http://localhost:52217/Service1/help