After. NET 3.5, WCF provides a WebGet method that allows access to Web services through URLs. In the previous code, I wrote similar examples many times, but I always forgot how to configure them. Now I will record the setup steps as follows:
- Set the endpoint communication protocolWebHttpBinding
- Set the endpoint behavior<WebHttp/>
- AddWebGetAttributes
The sample code is as follows: configure the web. config file
<system.serviceModel>
<services>
<service name="Services.ShowerService">
<endpoint binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="Services.IShowerService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
For the settings of the WCF interface, the URI template (UriTemplate) and JSON (WebMessageFormat. Json) are supported here:
namespace Services
{
[ServiceContract]
public interface ShowerService
{
[OperationContract]
[WebGet(UriTemplate="/Hello/{name}", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
string Hello(string name);
}
}
Test: Open IE and enter http: // localhost: 3000/Services/ShowerService. svc/hello/abc in the address bar. The accessed result is displayed. Debugging:
<WebHttp/>Change
<WebHttp helpEnabled = "true"/>Lists available interfaces on the browser page and provides submitted data samples. Open IE and enter http: // localhost: 3000/Services/ShowerService. svc/help in the address bar. Siverlight access: when using the SL WebClient to access the WebInvoke method, do not forget to set HttpRequestHeader. ContentType to application/json. The Code is as follows:
WebClient client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/json";