WCF Restful Service Demo

SVC

 public class WCFRestfulService : IWCFRestfulService 
    {
       public string GetServiceName(int Id)
        {
            return "This is a WCF Restful Service";
        }
    }

介面

[ServiceContract(Name = "WCRestfulService ")]
public interface IWCFRestfulService 
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetServiceName?Id={Id}")]
    string GetServiceName(int Id);
}

svc Markup(右鍵單擊 svc 檔案並單擊檢視 MarkUp)

<%@ ServiceHost Language="C#" Debug="true" Service="NamespaceName.WCFRestfulService" CodeBehind="WCFRestfulService.svc.cs" %>

Web 配置

<services>
      <service name="NamespaceName.WCFRestfulService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="NamespaceName.IWCFRestfulService" behaviorConfiguration="web"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

現在只需在埠中執行服務或主機即可。並使用“ http:// hostname / WCFRestfulService / GetServiceName?Id = 1 ” 訪問服務