[WCF] hosting

Source: Internet
Author: User
The WCF Service cannot exist independently. It must reside in a Windows process. We call this Windows process a host process. A host process can reside in multiple WCF services, and a WCF Service can exist in multiple host processes. The host process can even be a client process of WCF. The host process can either reside or be an application through IIS, was (Windows Vista) Program (Self-developed ).

 

IIS hosting

The main advantage of resident in IIS is that the host process starts when the client requests the service for the first time and IIS manages the lifecycle of the host process. The disadvantage is that once you select to reside in IIS, you can only select HTTP for data transmission. If you reside in iis5, another restriction is that all services can only use the same port number.

 

Follow these steps to create an IIS hosting:

1. Create a web site (WCF Service) in vs2005)

2. Modify the. SVC file under the site

<% @ Servicehost
Language = "C #"

DEBUG = "true"

Service = "anrs. Service. anrsservice"

Codebehind = "~ /App_code/services/anrsservice. cs"
%>

The content of the. SVC file is very simple, that is, the file that identifies the service and service. Note that the Service namespace is the namespace specified in the servicecontract feature, rather than the interface namespace.

3. Modify the Web. config configuration file

<? XML version = " 1.0 " ?>
< Configuration xmlns = " Http://schemas.microsoft.com/.NetConfiguration/v2.0 " >
< System. servicemodel >
< Services >
< Service name = " Anrs. Service. anrsservice " Behaviorconfiguration = " Returnfaults " >
< Endpoint contract = " Anrs. Service. ianrsservicecontract1 " Binding = " Wshttpbinding " />
</ Service >
</ Services >
< Behaviors >
< Servicebehaviors >
< Behavior name = " Returnfaults "   >
< Servicedebug includeexceptiondetailinfaults = " True "   />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
</ System. servicemodel >
< System. Web >
< Compilation debug = " True " />
</ System. Web >
</ Configuration >

Like the. SVC file, pay attention to the namespace specified by the name attribute value of the service node. After all the settings are OK, if you access the. SVC file in the browser, you can see the following effect.


 

Self-hosting

Creating a self-hosting in vs2005 is also very simple. The following is a typical example of Console self-hosting.

Using System;
Using System. servicemodel;

Namespace Anrs. Service
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Uri baseaddress =   New Uri ( " Http: // localhost: 8086/ " );
Servicehost sh =   New Servicehost ( Typeof (Anrsservice), baseaddress );

Sh. open ();

Console. Write ("Press any key to exit");
Console. Readline ();

Sh. Close ();
}
}
}

Servicehost
The class is derived from the servicehostbase abstract class, and the latter is also derived from the communicationobject abstract class, while the communicationobject implements the icommunicationobject interface. The icommunicationobject interface is defined as follows:

Public   Interface Icommunicationobject
{
Void Open ();
Void Close ();
Void Abort ();

event eventhandler closed;
event eventhandler closing;
event eventhandler faulted;
event eventhandler opened;
event eventhandler opening;

Iasyncresult beginclose (asynccallback callback,ObjectState );
Iasyncresult beginopen (asynccallback callback,ObjectState );
VoidEndclose (iasyncresult result );
VoidEndopen (iasyncresult result );

Communicationstate state
{ Get ;}
// More members
}
Public   Enum Communicationstate
{
Created,
Opening,
Opened,
Closing,
Closed,
Faulted
}

The interface defines the beginclose/beginopen methods. By calling these two methods, you can perform some asynchronous operations during the open/close service.

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.