为了配置 WCF REST 服务,您需要在 web.config 文件中添加一些内容
1)声明您的服务及其端点
<services>
<service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
behaviorConfiguration="webHttp"/>
</service>
</services>
服务名称将为 [项目名称]。[服务名称] 行为配置将与您在下一步中声明的行为名称相同。绑定必须是 webHttpBinding,因为您希望它为 REST。如果您想要 SOAP,则声明为 basicHttpBinding 合同是 [项目名称]。[接口名称] 端点中的行为配置将是您在下一步中声明的名称
2)声明服务行为(通常默认)
<behavior name="ServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
行为名称可以是任何名称,但它将用于匹配您在步骤 1 中声明的 BehaviorConfiguration 保留其余部分
3) 声明你的端点行为
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
行为名称可以是任何名称,但它将用于匹配端点中的行为配置。
最后,对于一个简单的 REST 服务,web.config 应该是这样的:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
behaviorConfiguration="webHttp"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>