14 of the WCF Service-Oriented Application series: hosting-Self-hosting)

Source: Internet
Author: User

Self-hosting is the life cycle of the host process provided and managed by developers. The self-managed mode applies to the following scenarios: When you need to determine the process (or machine) boundary between the client and the server, use in-process hosting, that is, when the service and the client process the same process. The self-managed process can be a console applicationProgramWindows applications, Windows Services, etc. The process must run before the client calls the service.

Self-managed communication protocols include HTTP, TCP, IPC, and MSMQ.

The self-managed host process must register the service type explicitly at run time and open the host for the client call. Therefore, the host process must run before the client call arrives. The method for creating a host process is usually to call the servicehost class in the main () method.

Next we will introduce self-hosting through a demo ).

Development Environment: Visual Studio 2010 + Net Framework 4.0.

1. Create a WCF Service library, mainlyCodeAs follows:

Interface Contract Code

  Namespace  Servicelibrary
{
// Servicecontract is a service contract
[Servicecontract (namespace = " Http://schemas.xinhaijulan.com/demos/SelfHost " )]
Public Interface Ihellowcf
{
// Operationcontract is a method contract
[Operationcontract]
String Getmessage ( String MSG );

[Operationcontract]
Item testdatacontract (item );
}
}

 

Implementation Code

  Namespace  Servicelibrary
{
Public Class Hellowcf: ihellowcf
{
Public String Getmessage ( String MSG)
{
Return String . Format ( " The server received ed message is: {0} " , MSG );
}


Public Item testdatacontract (item)
{
If (Item ! = Null )
{
Item. Name = " Item from client is not null. " ;
}
Else
{
Item = New Item ( 0 );
Item. Name = " Item from client is null. " ;
}

Return Item;
}
}
}

 

Data Contract Code

  Namespace  Servicelibrary
{
[Datacontract (Name = "item ",Namespace = " Http://schemas.xinhaijulan.com/demos/SelfHost " )]
Public Class Item
{
Public Item ( Int _ Id)
{
ID = _ Id;
}

[Datamember (Order = 0 )]
Public Int Id { Get ; Set ;}

[Datamember (Order = 1 )]
Public String Name { Get ; Set ;}
}
}

The above is the service library section. To start the server, we need to display the registered service type.

2. Create a server console project as a managed process project and add servicelibrary reference. The main code is as follows:

  Namespace  Server
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Using (Servicehost host = New Servicehost ( Typeof (Servicelibrary. hellowcf )))
{
Host. addserviceendpoint ( Typeof (Servicelibrary. ihellowcf ), New Nettcpbinding (), " Net. TCP: // localhost: 9000/hellowcf " );
Host. open ();
Console. writeline ( " Please input exit to close host. " );
String Key = Console. Readline ();
While (Key. tolower () ! = " Exit " )
{
Console. writeline ( " Please input exit to close host. " );
Key = Console. Readline ();
}
}
}
}
}

Here we use nettcpbinding to bind the address: net. TCP: // localhost: 9000/hellowcf. We can also configure the binding and address in the app. config file.

3. Create a client project and add a servicelibrary reference. In the previous project, we add a service reference to call the server. Here we use code to call the server.

 

    ///     <Summary>  
/// Test servicelibrary
/// </Summary>
/// <Param name = "ARGs"> </param>
Static Void Main ( String [] ARGs)
{
Channelfactory < Servicelibrary. ihellowcf > Channelfactory = New Channelfactory < Servicelibrary. ihellowcf > ( New Nettcpbinding ());
Servicelibrary. ihellowcf Client = Channelfactory. createchannel ( New Endpointaddress ( " Net. TCP: // localhost: 9000/hellowcf " ));

Console. writeline ( " ------------ Begin ------------- " );

Console. writeline (client. getmessage ( " Hello WCF! " ));

Servicelibrary. item = New Servicelibrary. Item ( 1 );
Item = Client. testdatacontract (item );
Console. writeline ( " Item. ID: " + Item. ID );
Console. writeline ( " Item. Name: " + Item. Name );

Console. writeline ( " ------------ End --------------- " );

Console. Readline ();
}

Because the server uses nettcpbinding to bind with the endpoint address: net. TCP: // localhost: 9000/hellowcf, we use the same binding protocol and address as the server when creating the call object on the client.

4. Start the server, right-click the server, and choose debug> Start new instance. For example:

5. Start the client, right-click the client and choose debug> Start new instance. The output is as follows:

   code highlighting produced by actipro codehighlighter (freeware) 
http://www.CodeHighlighter.com/
--> ------------ begin -----------
the server received ed message is : Hello WCF !
item. ID: 1
item. name: item from client is not null .
------------ end -------------

At this point, a self-managed demo in this chapter has been introduced. The main points are as follows:

The server uses the console application to host the WCF process servicehost;

The server uses code to write the binding and Endpoint address (or through configuration );

The client calls the server through code;

The client can write the binding and Endpoint addresses in code mode, which must be consistent with the server (or through configuration );

The server must be started before the client can call the server.

Click to download the demo.

 
  Author: Xinhai julan
Source: http: xinhaijulan.cnblogs.com
The copyright is shared by the author and the blog. You are welcome to reprint the copyright. However, you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

 

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.