ServiceHost ServiceHost ServiceHost ServiceHost Class

定义

提供服务的主机。Provides a host for services.

public ref class ServiceHost : System::ServiceModel::ServiceHostBase
public class ServiceHost : System.ServiceModel.ServiceHostBase
type ServiceHost = class
    inherit ServiceHostBase
Public Class ServiceHost
Inherits ServiceHostBase
继承
派生

示例

// Host the service within this EXE console application.
public static void Main()
{
  using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
  {
    try
    {
      // Open the ServiceHost to start listening for messages.
      serviceHost.Open();

        // The service can now be accessed.
      Console.WriteLine("The service is ready.");
      Console.WriteLine("Press <ENTER> to terminate service.");
      Console.ReadLine();

      // Close the ServiceHost.
      serviceHost.Close();
    }
    catch (TimeoutException timeProblem)
    {
      Console.WriteLine(timeProblem.Message);
      Console.ReadLine();
    }
    catch (CommunicationException commProblem)
    {
      Console.WriteLine(commProblem.Message);
      Console.ReadLine();
    }
  }
}
' Host the service within this EXE console application.
Public Shared Sub Main()
    ' Create a ServiceHost for the CalculatorService type and use the base address from config.
    Using svcHost As New ServiceHost(GetType(CalculatorService))
        Try
            ' Open the ServiceHost to start listening for messages.
            svcHost.Open()

            ' The service can now be accessed.
            Console.WriteLine("The service is ready.")
            Console.WriteLine("Press <ENTER> to terminate service.")
            Console.WriteLine()
            Console.ReadLine()

            'Close the ServiceHost.
            svcHost.Close()

        Catch timeout As TimeoutException
            Console.WriteLine(timeout.Message)
            Console.ReadLine()
        Catch commException As CommunicationException
            Console.WriteLine(commException.Message)
            Console.ReadLine()
        End Try
    End Using

End Sub

注解

实现 Windows Communication Foundation (WCF) 服务模型编程模型使用的主机。Implements the host used by the Windows Communication Foundation (WCF) service model programming model.

当您没有使用 Internet 信息服务 (IIS) 或 Windows 激活服务 (WAS) 公开服务时,请使用 ServiceHost 类来配置和公开服务以供客户端应用程序使用。Use the ServiceHost class to configure and expose a service for use by client applications when you are not using Internet Information Services (IIS) or Windows Activation Services (WAS) to expose a service. IIS 和 WAS 均代表您与 ServiceHost 对象交互。Both IIS and WAS interact with a ServiceHost object on your behalf.

若要公开以供调用方服务,WCF 需要完整的服务说明 (由ServiceDescription类)。To expose a service for use by callers, WCF requires a complete service description (represented by the ServiceDescription class). ServiceHost 类根据服务类型和配置信息创建 ServiceDescription,然后再使用该说明为说明中的每个终结点创建 ChannelDispatcher 对象。The ServiceHost class creates a ServiceDescription from the service type and configuration information and then uses that description to create ChannelDispatcher objects for each endpoint in the description.

使用 ServiceHost 对象加载服务、配置终结点、应用安全设置并启用侦听程序来处理传入的请求。Use a ServiceHost object to load a service, configure endpoints, apply security settings, and start listeners to handle incoming requests.

构造函数

ServiceHost() ServiceHost() ServiceHost() ServiceHost()

初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class.

ServiceHost(Object, Uri[]) ServiceHost(Object, Uri[]) ServiceHost(Object, Uri[]) ServiceHost(Object, Uri[])

使用服务的实例及其指定的基址初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class with the instance of the service and its base addresses specified.

ServiceHost(Type, Uri[]) ServiceHost(Type, Uri[]) ServiceHost(Type, Uri[]) ServiceHost(Type, Uri[])

使用服务的类型及其指定的基址初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class with the type of service and its base addresses specified.

属性

Authentication Authentication Authentication Authentication

获取服务身份验证行为。Gets the service authentication behavior.

(Inherited from ServiceHostBase)
Authorization Authorization Authorization Authorization

获取所承载服务的授权行为。Gets the authorization behavior for the service hosted.

(Inherited from ServiceHostBase)
BaseAddresses BaseAddresses BaseAddresses BaseAddresses

获取所承载服务使用的基址。Gets the base addresses used by the hosted service.

(Inherited from ServiceHostBase)
ChannelDispatchers ChannelDispatchers ChannelDispatchers ChannelDispatchers

获取服务主机所使用的通道调度程序的集合。Gets the collection of channel dispatchers used by the service host.

(Inherited from ServiceHostBase)
CloseTimeout CloseTimeout CloseTimeout CloseTimeout

获取或设置允许服务主机关闭的时间间隔。Gets or sets the interval of time allowed for the service host to close.

(Inherited from ServiceHostBase)
Credentials Credentials Credentials Credentials

获取所承载服务的凭据。Gets the credential for the service hosted.

(Inherited from ServiceHostBase)
DefaultCloseTimeout DefaultCloseTimeout DefaultCloseTimeout DefaultCloseTimeout

获取允许服务主机关闭的默认时间间隔。Gets the default interval of time allowed for the service host to close.

(Inherited from ServiceHostBase)
DefaultOpenTimeout DefaultOpenTimeout DefaultOpenTimeout DefaultOpenTimeout

获取允许服务主机打开的默认时间间隔。Gets the default interval of time allowed for the service host to open.

(Inherited from ServiceHostBase)
Description Description Description Description

获取所承载服务的说明。Gets the description of the service hosted.

(Inherited from ServiceHostBase)
Extensions Extensions Extensions Extensions

获取当前指定的服务主机的扩展。Gets the extensions for the current specified service host.

(Inherited from ServiceHostBase)
ImplementedContracts ImplementedContracts ImplementedContracts ImplementedContracts

检索由所承载服务实现的协定。Retrieves the contracts implemented by the service hosted.

(Inherited from ServiceHostBase)
IsDisposed IsDisposed IsDisposed IsDisposed

获取一个值,该值指示通信对象是否已被释放。Gets a value that indicates whether the communication object has been disposed.

(Inherited from CommunicationObject)
ManualFlowControlLimit ManualFlowControlLimit ManualFlowControlLimit ManualFlowControlLimit

获取或设置由所承载服务接收的消息的流控制限制。Gets or sets the flow control limit for messages received by the service hosted.

(Inherited from ServiceHostBase)
OpenTimeout OpenTimeout OpenTimeout OpenTimeout

获取或设置允许服务主机打开的时间间隔。Gets or sets the interval of time allowed for the service host to open.

(Inherited from ServiceHostBase)
SingletonInstance SingletonInstance SingletonInstance SingletonInstance

获取承载服务的单一实例。Gets the singleton instance of the hosted service.

State State State State

获取指示通信对象当前状态的值。Gets a value that indicates the current state of the communication object.

(Inherited from CommunicationObject)
ThisLock ThisLock ThisLock ThisLock

获取在状态转换过程中保护类实例的相互排斥锁。Gets the mutually exclusive lock that protects the class instance during a state transition.

(Inherited from CommunicationObject)

方法

Abort() Abort() Abort() Abort()

使通信对象立即从其当前状态转换到正在关闭状态。Causes a communication object to transition immediately from its current state into the closing state.

(Inherited from CommunicationObject)
AddBaseAddress(Uri) AddBaseAddress(Uri) AddBaseAddress(Uri) AddBaseAddress(Uri)

将基址添加到服务主机。Adds a base address to the service host.

(Inherited from ServiceHostBase)
AddDefaultEndpoints() AddDefaultEndpoints() AddDefaultEndpoints() AddDefaultEndpoints()

使用默认绑定为在服务主机中找到的每个协定中的所有基址添加服务终结点。Adds service endpoints for all base addresses in each contract found in the service host with the default binding.

(Inherited from ServiceHostBase)
AddServiceEndpoint(ServiceEndpoint) AddServiceEndpoint(ServiceEndpoint) AddServiceEndpoint(ServiceEndpoint) AddServiceEndpoint(ServiceEndpoint)

将指定的服务终结点添加到承载服务中。Adds the specified service endpoint to the hosted service.

(Inherited from ServiceHostBase)
AddServiceEndpoint(String, Binding, String) AddServiceEndpoint(String, Binding, String) AddServiceEndpoint(String, Binding, String) AddServiceEndpoint(String, Binding, String)

使用指定的协定、绑定和终结点地址将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.

(Inherited from ServiceHostBase)
AddServiceEndpoint(String, Binding, String, Uri) AddServiceEndpoint(String, Binding, String, Uri) AddServiceEndpoint(String, Binding, String, Uri) AddServiceEndpoint(String, Binding, String, Uri)

使用指定的协定、绑定和终结点地址以及包含服务所侦听地址的 URI,将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, endpoint address and URI that contains the address at which it listens.

(Inherited from ServiceHostBase)
AddServiceEndpoint(String, Binding, Uri) AddServiceEndpoint(String, Binding, Uri) AddServiceEndpoint(String, Binding, Uri) AddServiceEndpoint(String, Binding, Uri)

使用指定的协定、绑定和包含终结点地址的 URI 将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, and a URI that contains the endpoint address.

(Inherited from ServiceHostBase)
AddServiceEndpoint(String, Binding, Uri, Uri) AddServiceEndpoint(String, Binding, Uri, Uri) AddServiceEndpoint(String, Binding, Uri, Uri) AddServiceEndpoint(String, Binding, Uri, Uri)

使用指定的协定、绑定和包含终结点与所侦听地址的 URI,将服务终结点添加到承载服务。Adds a service endpoint to the hosted service with the specified contract, binding, and URIs that contain the endpoint and listening addresses.

(Inherited from ServiceHostBase)
AddServiceEndpoint(Type, Binding, String) AddServiceEndpoint(Type, Binding, String) AddServiceEndpoint(Type, Binding, String) AddServiceEndpoint(Type, Binding, String)

使用指定的协定、绑定和终结点地址将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.

AddServiceEndpoint(Type, Binding, String, Uri) AddServiceEndpoint(Type, Binding, String, Uri) AddServiceEndpoint(Type, Binding, String, Uri) AddServiceEndpoint(Type, Binding, String, Uri)

使用指定的协定、绑定和终结点地址以及服务在其上侦听的 URI 将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, an endpoint address, and a URI on which the service listens.

AddServiceEndpoint(Type, Binding, Uri) AddServiceEndpoint(Type, Binding, Uri) AddServiceEndpoint(Type, Binding, Uri) AddServiceEndpoint(Type, Binding, Uri)

使用指定的协定、绑定和包含终结点地址的 URI 将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, and URI that contains the endpoint address.

AddServiceEndpoint(Type, Binding, Uri, Uri) AddServiceEndpoint(Type, Binding, Uri, Uri) AddServiceEndpoint(Type, Binding, Uri, Uri) AddServiceEndpoint(Type, Binding, Uri, Uri)

使用指定的协定、绑定、包含终结点地址的 URI 以及服务在其上侦听的 URI 将服务终结点添加到承载服务中。Adds a service endpoint to the hosted service with a specified contract, binding, a URI that contains the endpoint address, and a URI on which the service listens.

ApplyConfiguration() ApplyConfiguration() ApplyConfiguration() ApplyConfiguration()

从配置文件加载服务说明,并将其应用于正在构造的运行库。Loads the service description from the configuration file and applies it to the runtime being constructed.

BeginClose(AsyncCallback, Object) BeginClose(AsyncCallback, Object) BeginClose(AsyncCallback, Object) BeginClose(AsyncCallback, Object)

开始一个异步操作以关闭通信对象。Begins an asynchronous operation to close a communication object.

(Inherited from CommunicationObject)
BeginClose(TimeSpan, AsyncCallback, Object) BeginClose(TimeSpan, AsyncCallback, Object) BeginClose(TimeSpan, AsyncCallback, Object) BeginClose(TimeSpan, AsyncCallback, Object)

开始一个异步操作以在指定超时内关闭通信对象。Begins an asynchronous operation to close a communication object with a specified timeout.

(Inherited from CommunicationObject)
BeginOpen(AsyncCallback, Object) BeginOpen(AsyncCallback, Object) BeginOpen(AsyncCallback, Object) BeginOpen(AsyncCallback, Object)

开始一个异步操作以打开通信对象。Begins an asynchronous operation to open a communication object.

(Inherited from CommunicationObject)
BeginOpen(TimeSpan, AsyncCallback, Object) BeginOpen(TimeSpan, AsyncCallback, Object) BeginOpen(TimeSpan, AsyncCallback, Object) BeginOpen(TimeSpan, AsyncCallback, Object)

开始一个异步操作以在指定时间间隔内打开通信对象。Begins an asynchronous operation to open a communication object within a specified interval of time.

(Inherited from CommunicationObject)
Close() Close() Close() Close()

使通信对象从其当前状态转换到关闭状态。Causes a communication object to transition from its current state into the closed state.

(Inherited from CommunicationObject)
Close(TimeSpan) Close(TimeSpan) Close(TimeSpan) Close(TimeSpan)

使通信对象在指定时间间隔内从当前状态转换到关闭状态。Causes a communication object to transition from its current state into the closed state within a specified interval of time.

(Inherited from CommunicationObject)
CreateDescription(IDictionary<String,ContractDescription>) CreateDescription(IDictionary<String,ContractDescription>) CreateDescription(IDictionary<String,ContractDescription>) CreateDescription(IDictionary<String,ContractDescription>)

创建承载服务的说明。Creates a description of the service hosted.

EndClose(IAsyncResult) EndClose(IAsyncResult) EndClose(IAsyncResult) EndClose(IAsyncResult)

完成一个异步操作以关闭通信对象。Completes an asynchronous operation to close a communication object.

(Inherited from CommunicationObject)
EndOpen(IAsyncResult) EndOpen(IAsyncResult) EndOpen(IAsyncResult) EndOpen(IAsyncResult)

完成一个异步操作以打开通信对象。Completes an asynchronous operation to open a communication object.

(Inherited from CommunicationObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Fault() Fault() Fault() Fault()

使通信对象从其当前状态转换到出错状态。Causes a communication object to transition from its current state into the faulted state.

(Inherited from CommunicationObject)
GetCommunicationObjectType() GetCommunicationObjectType() GetCommunicationObjectType() GetCommunicationObjectType()

获取通信对象的类型。Gets the type of communication object.

(Inherited from CommunicationObject)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IncrementManualFlowControlLimit(Int32) IncrementManualFlowControlLimit(Int32) IncrementManualFlowControlLimit(Int32) IncrementManualFlowControlLimit(Int32)

以指定的增量增加对消息到所承载服务的流入率的限制。Increases the limit on the flow rate of messages to the hosted service by a specified increment.

(Inherited from ServiceHostBase)
InitializeDescription(Object, UriSchemeKeyedCollection) InitializeDescription(Object, UriSchemeKeyedCollection) InitializeDescription(Object, UriSchemeKeyedCollection) InitializeDescription(Object, UriSchemeKeyedCollection)

基于承载服务的实例和指定的基址初始化该承载服务的说明。Initializes a description of the service hosted based on its instance and specified base addresses.

InitializeDescription(Type, UriSchemeKeyedCollection) InitializeDescription(Type, UriSchemeKeyedCollection) InitializeDescription(Type, UriSchemeKeyedCollection) InitializeDescription(Type, UriSchemeKeyedCollection)

基于承载服务的类型和指定的基址初始化该承载服务的说明。Initializes a description of the service hosted based on its type and specified base addresses.

InitializeDescription(UriSchemeKeyedCollection) InitializeDescription(UriSchemeKeyedCollection) InitializeDescription(UriSchemeKeyedCollection) InitializeDescription(UriSchemeKeyedCollection)

使用协定和服务说明创建并初始化服务主机。Creates and initializes the service host with the contract and service descriptions.

(Inherited from ServiceHostBase)
InitializeRuntime() InitializeRuntime() InitializeRuntime() InitializeRuntime()

初始化服务主机的运行库。Initializes the runtime for the service host.

(Inherited from ServiceHostBase)
LoadConfigurationSection(ServiceElement) LoadConfigurationSection(ServiceElement) LoadConfigurationSection(ServiceElement) LoadConfigurationSection(ServiceElement)

从承载服务的配置文件加载服务元素。Loads the service element from the configuration file of the hosted service.

(Inherited from ServiceHostBase)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnAbort() OnAbort() OnAbort() OnAbort()

中止服务。Aborts the service.

(Inherited from ServiceHostBase)
OnBeginClose(TimeSpan, AsyncCallback, Object) OnBeginClose(TimeSpan, AsyncCallback, Object) OnBeginClose(TimeSpan, AsyncCallback, Object) OnBeginClose(TimeSpan, AsyncCallback, Object)

开始在关闭服务主机时调用的异步操作。Begins an asynchronous operation invoked on the close of the service host.

(Inherited from ServiceHostBase)
OnBeginOpen(TimeSpan, AsyncCallback, Object) OnBeginOpen(TimeSpan, AsyncCallback, Object) OnBeginOpen(TimeSpan, AsyncCallback, Object) OnBeginOpen(TimeSpan, AsyncCallback, Object)

开始在打开服务主机时调用的异步操作。Begins an asynchronous operation invoked on the opening of the service host.

(Inherited from ServiceHostBase)
OnClose(TimeSpan) OnClose(TimeSpan) OnClose(TimeSpan) OnClose(TimeSpan)

关闭所承载服务,包括它们的通道调度程序以及相关的实例上下文和侦听器。Closes down the hosted service, including their channel dispatchers and associated instance contexts and listeners.

(Inherited from ServiceHostBase)
OnClosed() OnClosed() OnClosed() OnClosed()

当服务主机关闭时,释放所承载的可处置服务。Disposes of disposable services that are being hosted when the service host is closed.

OnClosing() OnClosing() OnClosing() OnClosing()

在通信对象转换到正在关闭状态的过程中被调用。Invoked during the transition of a communication object into the closing state.

(Inherited from CommunicationObject)
OnEndClose(IAsyncResult) OnEndClose(IAsyncResult) OnEndClose(IAsyncResult) OnEndClose(IAsyncResult)

完成在关闭服务主机时调用的异步操作。Completes an asynchronous operation invoked on the closing of the service host.

(Inherited from ServiceHostBase)
OnEndOpen(IAsyncResult) OnEndOpen(IAsyncResult) OnEndOpen(IAsyncResult) OnEndOpen(IAsyncResult)

完成在打开服务主机时调用的异步操作。Completes an asynchronous operation invoked on the opening of the service host.

(Inherited from ServiceHostBase)
OnFaulted() OnFaulted() OnFaulted() OnFaulted()

在调用了同步错误操作,从而引起通信对象转换为出错状态的情况下,该方法插入对通信对象的处理。Inserts processing on a communication object after it transitions to the faulted state due to the invocation of a synchronous fault operation.

(Inherited from CommunicationObject)
OnOpen(TimeSpan) OnOpen(TimeSpan) OnOpen(TimeSpan) OnOpen(TimeSpan)

打开通道调度程序。Opens the channel dispatchers.

(Inherited from ServiceHostBase)
OnOpened() OnOpened() OnOpened() OnOpened()

获取所承载服务的服务凭据、服务身份验证和授权行为。Gets the service credentials,service authentication and authorization behavior for the hosted service.

(Inherited from ServiceHostBase)
OnOpening() OnOpening() OnOpening() OnOpening()

在通信对象转换到正在打开状态的过程中被调用。Invoked during the transition of a communication object into the opening state.

(Inherited from CommunicationObject)
Open() Open() Open() Open()

使通信对象从已创建状态转换到已打开状态。Causes a communication object to transition from the created state into the opened state.

(Inherited from CommunicationObject)
Open(TimeSpan) Open(TimeSpan) Open(TimeSpan) Open(TimeSpan)

使通信对象在指定时间间隔内从已创建状态转换到已打开状态。Causes a communication object to transition from the created state into the opened state within a specified interval of time.

(Inherited from CommunicationObject)
ReleasePerformanceCounters() ReleasePerformanceCounters() ReleasePerformanceCounters() ReleasePerformanceCounters()

释放所承载服务的服务与通道调度程序性能计数器。Releases the service and channel dispatcher performance counters for the hosted service.

(Inherited from ServiceHostBase)
SetEndpointAddress(ServiceEndpoint, String) SetEndpointAddress(ServiceEndpoint, String) SetEndpointAddress(ServiceEndpoint, String) SetEndpointAddress(ServiceEndpoint, String)

将指定终结点的终结点地址设置为指定的地址。Sets the endpoint address of the specified endpoint to the specified address.

(Inherited from ServiceHostBase)
ThrowIfDisposed() ThrowIfDisposed() ThrowIfDisposed() ThrowIfDisposed()

如果通信对象已释放,则引发异常。Throws an exception if the communication object is disposed.

(Inherited from CommunicationObject)
ThrowIfDisposedOrImmutable() ThrowIfDisposedOrImmutable() ThrowIfDisposedOrImmutable() ThrowIfDisposedOrImmutable()

如果通信对象的 State 属性未设置为 Created 状态,则引发异常。Throws an exception if the communication object the State property is not set to the Created state.

(Inherited from CommunicationObject)
ThrowIfDisposedOrNotOpen() ThrowIfDisposedOrNotOpen() ThrowIfDisposedOrNotOpen() ThrowIfDisposedOrNotOpen()

如果通信对象未处于 Opened 状态,则引发异常。Throws an exception if the communication object is not in the Opened state.

(Inherited from CommunicationObject)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

事件

Closed Closed Closed Closed

当通信对象转换到已关闭状态时发生。Occurs when a communication object transitions into the closed state.

(Inherited from CommunicationObject)
Closing Closing Closing Closing

当通信对象转换到正在关闭状态时发生。Occurs when a communication object transitions into the closing state.

(Inherited from CommunicationObject)
Faulted Faulted Faulted Faulted

在通信对象转换到出错状态时发生。Occurs when a communication object transitions into the faulted state.

(Inherited from CommunicationObject)
Opened Opened Opened Opened

当通信对象转换到已打开状态时发生。Occurs when a communication object transitions into the opened state.

(Inherited from CommunicationObject)
Opening Opening Opening Opening

当通信对象转换到正在打开状态时发生。Occurs when a communication object transitions into the opening state.

(Inherited from CommunicationObject)
UnknownMessageReceived UnknownMessageReceived UnknownMessageReceived UnknownMessageReceived

接收未知消息时发生。Occurs when an unknown message is received.

(Inherited from ServiceHostBase)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

关闭服务主机。Closes the service host.

(Inherited from ServiceHostBase)

适用于