当前位置: 移动技术网 > IT编程>开发语言>.net > How to create RESTful WCF Service

How to create RESTful WCF Service

2017年12月27日  | 移动技术网IT编程  | 我要评论

治疗兔子,猜字谜七十二小时,rootnature

/*by garcon1986*/

 

Based on my last post about how to create, host, test and consume WCF Service, here we will discover how to create RESTful WCF Service.

 

“Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. REST has emerged as a predominant Web service design model.” from wiki

 

“Those principles of REST service are:

User agents interact with resources, and resources are anything that can be named and represented. Each resource can be addressed via a unique Uniform Resource Identifier (URI).

Interaction with resources (located through their unique URIs) is accomplished using a uniform interface of the HTTP standard verbs (GET, POST, PUT, and DELETE). Also important in the interaction is the declaration of the resource's media type, which is designated using the HTTP Content-Type header. (XHTML, XML, JPG, PNG, and JSON are some well-known media types.)

Resources are self-descriptive. All the information necessary to process a request on a resource is contained inside the request itself (which allows services to be stateless).

Resources contain links to other resources (hyper-media).” from microsoft

 

We don't need to change our class "HelloWorldService", so it should always be:

[csharp]  

namespace MyWCFServices  

{  

    /// <summary>  

    /// Create a service class and implement service interface  

    /// </summary>  

    public class HelloWorldService : IHelloWorldService  

    {  

        public string GetMessage(string name)  

        {  

            return "Hello world from " + name + "!";  

        }  

  

        public string GetPerson(string person)  

        {  

            return "Person name :" + person + "!";  

        }  

    }  

}  

 

While, the inteface "IHelloWorldService", should be modified.

[csharp]  

using System.ServiceModel; //used for ServiceContract. it contains the types necessary to build Windows Communication Foundation (WCF) service and client applications.  

using System.ServiceModel.Web;  

  

namespace MyWCFServices  

{  

    [ServiceContract]  

    public interface IHelloWorldService  

    {  

        [OperationContract]  

        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml,   

            BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/XML/{name}")]  

        string GetMessage(string name);  

  

        [OperationContract]  

        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,  

            BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/JSON/{person}")]  

        string GetPerson(string person);  

    }  

}  

 

The content of "HelloWorldService.svc" should always be :

[html]  

<!--Host web service-->  

<%@ServiceHost Service="MyWCFServices.HelloWorldService" %>  

 

And then we should modify the web.config of "HostDevServer"

[html]  

<?xml version="1.0" encoding="utf-8"?>  

  

<!--  

  Pour plus d'informations sur la configuration de votre application ASP.NET, consultez  

  http://go.microsoft.com/fwlink/?LinkId=169433  

  -->  

  

<configuration>  

  <!--Root node of config file-->  

  <system.web>  

    <compilation debug="true" targetFramework="4.0" />  

  </system.web>  

  

  <system.webServer>  

    <modules runAllManagedModulesForAllRequests="true" />  

  </system.webServer>  

  

  <system.serviceModel>  

    <!--Top node of WCF Service-->  

    <behaviors>  

      <serviceBehaviors>  

        <!--Specify the bahaviors of a service-->  

        <behavior name="MyServiceTypeBehaviors">  

          <!-- httpGetEnabled Enable other programs locate the metadata of this web service,   

          client applications can't generate proxy and use web service without medatadata-->  

          <serviceMetadata httpGetEnabled="true"/>  

  

          <serviceDebug includeExceptionDetailInFaults="false"/>  

        </behavior>  

      </serviceBehaviors>  

      <endpointBehaviors>  

        <behavior name="REST"> <!-- Important -->  

          <webHttp/>  

        </behavior>  

      </endpointBehaviors>  

    </behaviors>  

    <services>  

      <!--List hosted WCF Services in this web site-->  

      <!--Each service has its name, behavior and endpoint-->  

      <service name="MyWCFServices.HelloWorldService" behaviorConfiguration="MyServiceTypeBehaviors">  

        <!--Here wsHttpBinding should be replaced by webHttpBinding for REST Service, and behaviorConfiguration should be "REST" as defined in <endpointBehaviors>-->  

        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="MyWCFServices.IHelloWorldService"/>  

        <!--this endpoint is for metadata exchange-->  

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  

      </service>  

    </services>  

  </system.serviceModel>  

</configuration>  

 

We need to run the service in "HostDevServer" before we test in browser.

Firstly we'll test the first method "GetMessage", 

 

Then, we test the second method "GetPerson",

 

I hope this helps! Enjoy coding!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网