Web access method of WCF

Source: Internet
Author: User
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:
  1. Set the endpoint communication protocolWebHttpBinding
  2. Set the endpoint behavior<WebHttp/>
  3. 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";
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.