现在的位置: 主页 > 联系方式 > 文章正文

How to create RESTful WCF Service

作者:成都渝祥金属丝网制品有限公司 来源:www.cdyuxiang.com 未知发布时间:2017-09-09 10:17:57
How to create RESTful WCF Service /*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">

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网页制作 https://www.feimao666.com

上一篇:做好5个方面 让用户依赖你的网站 下一篇:最后一页