Boarding mode of WCF in REST

Source: Internet
Author: User

 

Like the SOA-based WCF, the REST-based WCF also has a variety of boarding methods, such as IIS boarding and self-boarding, even if it only has one protocol.
REST is based on the HTTP protocol. Therefore, in this architecture, the Web server must be supported for WCF hosting. Obviously, Microsoft will certainly use
Your Web server is IIS.
This section contains the following contents:
1. IIS boarding
2. host the console Program (also known as self-host)
Of course, rest wcf also has other boarding methods. Here I will only pick out two typical methods for you to introduce. If you are interested, try other boarding methods.
The instances used in this section are the examples used in the previous section. The Demo structure is as follows:

Structure Description: the Client is the service consumer. The Contracts defines the service contract and data contract, and the Services defines the Service implementation. SelfHost and WebHost are self-hosted and IIS host Program.
Use the same service contract in IIS or on-premises server. The service contract is defined as follows:

[ServiceContract]public interface ILog{[OperationContract][WebGet(UriTemplate = "/")]List<LogEntity> GetAll();[OperationContract][WebGet(UriTemplate = "/Log/{year}/{month}")]List<LogEntity> GetMonthLog(string year, string month);}

  

Data contract definition:

[DataContract]public class LogEntity{[DataMember]public int ID { get; set; }[DataMember]public string EventName { get; set; }[DataMember]public string Level { get; set; }[DataMember]public DateTime Time { get; set; }}

  

Service implementation code:

# Region ILog member public List <LogEntity> GetAll () {return GetLogEntityList ();} public List <LogEntity> GetMonthLog (string year, string month) {List <LogEntity> logEntities = GetLogEntityList (); List <LogEntity> logList = new List <LogEntity> (); logEntities. forEach (log => {if (log. time. year. toString () = year & log. time. month. toString () = month) {logList. add (log) ;}}); return logList ;}# endregion

  


1. IIS boarding.
As we all know, Web programs are based on the HTTP protocol, while REST is also based on HTTP. The REST architecture holds that the WEB is very successful, concise, and not complex. Therefore, based on
IIS is easy to understand. In the IIS host, I directly created a Web project and provided service calls in the WCF Service file.
The Code is as follows:

public List<LogEntity> GetAll(){LogServices services = new LogServices();return services.GetAll();}public List<LogEntity> GetMonthLog(string year, string month){LogServices services = new LogServices();return services.GetMonthLog(year, month);}

  

In this way, the interface specification defines the provision of external services.
2. host the console program.
2.1 implement rest wcf boarding by coding.
The boarding code is as follows:

Static void HostViaCode () {using (WebServiceHost host = new WebServiceHost (typeof (LogServices) {ServiceEndpoint endpoint = host. addServiceEndpoint (typeof (ILog), new WebHttpBinding (), "http: // 127.0.0.1: 6688/LogService"); endpoint. behaviors. add (new WebHttpBehavior (); Console. writeLine ("service enabled... "); host. open (); Console. readLine ();}}

  


2.2 implement rest wcf boarding through configuration.
First, configure related endpoints in the configuration file. The configuration file is as follows:

<system.serviceModel><behaviors><serviceBehaviors ><behavior name="RESTServiceBehavior"></behavior></serviceBehaviors><endpointBehaviors><behavior name="servierBehavior"></behavior></endpointBehaviors></behaviors><services><service name="Services.LogServices" behaviorConfiguration="RESTServiceBehavior"><endpoint address="Http://127.0.0.1:8866/LogService" contract="Contracts.ILog" binding="webHttpBinding" behaviorConfiguration="servierBehavior"></endpoint></service></services></system.serviceModel>

  

Those who have a little knowledge about the wcf soa architecture can understand it at a Glance. Yes, there is no difference between self-hosted hosting in the REST architecture and SOA.
The code for configuring the host is as follows:

Using (WebServiceHost host = new WebServiceHost (typeof (LogServices) {Console. writeLine ("service enabled... "); host. open (); Console. readLine ();}

  

In this way, you can host the rest wcf Service in the console program.
Call the GetAll interface in IE. The obtained data is as follows:


Call the GetMonthLog interface in IE to obtain the following data:

Finally, the Code for calling the service in the Client is as follows:

Private const string address = "Http: // 127.0.0.1: 8866/LogService"; static void Main (string [] args) {InvokeRESTService (); Console. readLine ();} static void InvokeRESTService () {HttpWebRequest webRequest = WebRequest. create (address) as HttpWebRequest; webRequest. method = "GET"; HttpWebResponse webResponse = webRequest. getResponse () as HttpWebResponse; using (StreamReader reader = new StreamReader (webResponse. getResponseStream () {Console. writeLine (string. format ("Get call result: {0}", reader. readToEnd ()));}}

  


Whether it is IIS boarding or self-boarding, the customer's monotonous use results in different boarding methods are as follows:

Note: If you host the rest wcf Service through the configuration method, an Asp. Net Development Server will be available even when the Server program starts.
But it does not affect the client's service calls. We can see that after the Server is started, if Asp. Net Development Server is disabled,
The client still calls the service successfully.

The preceding methods are used to host rest wcf in IIS and console programs. Similarly, it can also implement boarding in other programs, such as Windows Service programs and Winform programs.
In addition, I have a small change in the definition of the contract in the first three sections, that is, in [WebGet (UriTemplate = "/Log/{year}/{month}")] replace Get with Log to make it clearer.
Express REST ideas.

 

 

Author tyb1222

Related Article

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.