在前面的一些章节中,我们根据DEMO来演示WCF的相关特性,其中我们一直在用WCF服务端,然而WCF服务类不能凭空存在。每个WCF服务都必须进行托管(Hosting)在Windows进程中,该进程被称为宿主进程(Host Process)。
宿主进程与WCF服务的关系:单个宿主进程可以托管多个服务,而相同的服务类型也能够托管在多个宿主进程中。
WCF的托管的环境有很多种,下面介绍一些常用的托管环境:
IIS托管:在IIS中托管服务。优势为宿主进程可以在客户端提交第一次请求的时候自动启动,还可以借助IIS管理宿主进程的生命周期。缺点为只能使用HTTP协议,如果是IIS5还要受端口限制,要求所有服务必须使用相同的端口号。Web站点的配置文件(web.config)必须列出需要公开为服务的类型,类型使用类型全称,如果服务类型来自于一个没有被引用的程序集,则还要包括程序集名,配置如下:
< system.serviceModel >
< services >
< service name = " MyNamespace.MyService " >
...
</ service >
</ services >
</ system.serviceModel >
自托管:Self-Hosting,由开发者提供和管理宿主进程的生命周期。自托管进程可以是控制台应用程序、Windows应用程序、Windows服务等,进程必须在客户端调用服务之前运行。自托管支持通信的协议包括:HTTP、TCP、IPC、MSMQ。
自托管宿主进程必须在运行时显示地注册服务类型,同时为客户端的调用打开宿主,因此才要求宿主进程必须在客户端调用到达之前运行。创建宿主进程的方法通常是在Main() 方法中调用ServiceHost类。ServiceHost类的定义如下:
// Summary:
// Defines the contract for the basic state machine for all communication-oriented
// objects in the system, including channels, the channel managers, factories,
// listeners, and dispatchers, and service hosts.
public interface ICommunicationObject
{
// Summary:
// Gets the current state of the communication-oriented object.
//
// Returns:
// The value of the System.ServiceModel.CommunicationState of the object.
CommunicationState State { get ; }
// Summary:
// Occurs when the communication object completes its transition from the closing
// state into the closed state.
event EventHandler Closed;
//
// Summary:
// Occurs when the communication object first enters the closing state.
event EventHandler Closing;
//
// Summary:
// Occurs when the communication object first enters the faulted state.
event EventHandler Faulted;
//
// Summary:
// Occurs when the communication object completes its transition from the opening
// state into the opened state.
event EventHandler Opened;
//
// Summary:
// Occurs when the communication object first enters the opening state.
event EventHandler Opening;
// Summary:
// Causes a communication object to transition immediately from its current
// state into the closed state.
void Abort();
//
// Summary:
// Begins an asynchronous operation to close a communication object.
//
// Parameters:
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous close operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous close operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous close operation.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.BeginClose() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to close gracefully.
IAsyncResult BeginClose(AsyncCallback callback, object state);
//
// Summary:
// Begins an asynchronous operation to close a communication object with a specified
// timeout.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous close operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous close operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous close operation.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.BeginClose() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The specified timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to close gracefully.
IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state);
//
// Summary:
// Begins an asynchronous operation to open a communication object.
//
// Parameters:
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous open operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous open operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous open operation.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default open timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
IAsyncResult BeginOpen(AsyncCallback callback, object state);
//
// Summary:
// Begins an asynchronous operation to open a communication object within a
// specified interval of time.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous open operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous open operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous open operation.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The specified timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
//
// Summary:
// Causes a communication object to transition from its current state into the
// closed state.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.Close() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default close timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to close gracefully.
void Close();
//
// Summary:
// Causes a communication object to transition from its current state into the
// closed state.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.Close() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The timeout elapsed before the System.ServiceModel.ICommunicationObject was
// able to close gracefully.
void Close(TimeSpan timeout);
//
// Summary:
// Completes an asynchronous operation to close a communication object.
//
// Parameters:
// result:
// The System.IAsyncResult that is returned by a call to the System.ServiceModel.ICommunicationObject.BeginClose()
// method.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.BeginClose() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The timeout elapsed before the System.ServiceModel.ICommunicationObject was
// able to close gracefully.
void EndClose(IAsyncResult result);
//
// Summary:
// Completes an asynchronous operation to open a communication object.
//
// Parameters:
// result:
// The System.IAsyncResult that is returned by a call to the System.ServiceModel.ICommunicationObject.BeginOpen()
// method.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The timeout elapsed before the System.ServiceModel.ICommunicationObject was
// able to enter the System.ServiceModel.CommunicationState.Opened state and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
void EndOpen(IAsyncResult result);
//
// Summary:
// Causes a communication object to transition from the created state into the
// opened state.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default open timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
void Open();
//
// Summary:
// Causes a communication object to transition from the created state into the
// opened state within a specified interval of time.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The specified timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
void Open(TimeSpan timeout);
}
// Summary:
// Provides a common base implementation for the basic state machine common
// to all communication-oriented objects in the system, including channels,
// listeners, and the channel and listener factories.
public abstract class CommunicationObject : ICommunicationObject
{
...
}
// Summary:
// Extends the System.ServiceModel.ServiceHostBase class to implement hosts
// that expose custom programming models.
public abstract class ServiceHostBase : CommunicationObject, IExtensibleObject < ServiceHostBase > , IDisposable
{
...
}
// Summary:
// Provides a host for services.
public class ServiceHost : ServiceHostBase
{
...
}
创建ServiceHost对象时,需要为ServiceHost的构造函数提供服务类型,至于默认的基地址则是可选的。在宿主程序中,通过调用Open() 方法,可以允许调用伟入;通过调用Close() 方法终结宿主实例,完成进程的调用,通过情况下执行关闭操作会停止宿主进程,具体实例将在以后章节中介绍。
WAS托管:Windows激活服务(WAS)是一个系统服务,是IIS7的一部分,也可以独立的安装与配置。IIS与WAS的主要区别在于WAS并不局限使用HTTP,它支持所有可用的WCF传输协议、端口与队列,支持的协议包括:HTTP、TCP、IPC、MSMQ。
WAS提供了大量基于自托管的强大功能,包括应用程序池、回收机制、空闲时间管理(Idle Time Mannagement)、身份管理(Identity Management) 以及隔离(Isolation);宿主进程可以根据情况选择使用这些功能。
我们可以根据平台不同选择不同的托管环境:
Windows Server 2003:在IIS6上应用HTTP协议、在Windows服务上服用non-HTTP;
Windows Server 2008:在IIS7/WAS上可以应用所有的协议;
Windows XP SP2 与Windows Vista:客户端主机运行Windows应用程序或者Windows服务。
本章主要介绍WCF托管(Hosting)的基础知识(本章知识主要参考《WCF服务编程》),下一章我们通过一些DEMO对托管进行介绍。
作者:心海巨澜
出处: http://xinhaijulan.cnblogs.com
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。