WCFREST开发

首先新建wcf项目(WcfService1), 按照下面的步骤转换成支持REST

(1)
如果没有Global.asax 则新建, 内容如下:
需要新建两个文件 Global.asax  和 Global.asax.cs

1,用文本在项目下面添加文件 Global.asax  文件里面添加下面一行即可
<%@ Application Codebehind="Global.asax.cs" Inherits="WcfService1.Global" Language="C#" %>


2,用文本在项目下面添加类:  Global.asax.cs
using System;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Routing;

namespace RESTService1
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            // Edit the base address of Service1 by replacing the "Service1" string below
            RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
        }
    }
}


将上面两个文件包含到项目里面
需要引用包:  System.ServiceModel.Activation.dll


(2)
web.config 修改为如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
  </system.web>
  
  <system.serviceModel>  
    <behaviors>
      <serviceBehaviors>       
        <behavior>
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>     
    </behaviors>
    
    <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 helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

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

    <!--
        若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
        在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer> 
  

</configuration>


实际上是在<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 helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />
  </webHttpEndpoint>
</standardEndpoints>

在<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>



(3)
在接口里面的方法上添加
[WebGet(UriTemplate = "getData?value={value}", ResponseFormat = WebMessageFormat.Json)]
上面是get的添加方法,post例子: 
[WebInvoke(UriTemplate = "getData", Method = "POST", RequestFormat = WebMessageFormat.Json)]

例如:
[ServiceContract]
public interface IService1
{

	[WebGet(UriTemplate = "getData?value={value}", ResponseFormat=WebMessageFormat.Json)]
	[OperationContract]
	string GetData(int value);      

	[OperationContract]
	CompositeType GetDataUsingDataContract(CompositeType composite);

	// TODO: 在此添加您的服务操作
}



发布到IIS   (假如地址为: http://192.168.100.119:88/ )
在浏览器里面输入下面的连接看看
http://192.168.100.119:88/Service1/help

你可能感兴趣的:(WCF)