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 ” 访问服务