ServiceHost 类

参考网址:https://docs.microsoft.com/zh-cn/dotnet/api/system.servicemodel.servicehost?view=netframework-4.8

定义

提供服务的主机。

C#
public class ServiceHost : System.ServiceModel.ServiceHostBase
继承
派生

示例

C#
// 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();
    }
  }
}

注解

实现 Windows Communication Foundation (WCF) 服务模型编程模型所使用的主机。

使用 ServiceHost 类来配置和公开服务以供客户端应用程序使用,但不使用 Internet Information Services (IIS) 或 Windows 激活服务 () 公开服务。 IIS 和 WAS 均代表您与 ServiceHost 对象交互。

若要公开服务以供调用方使用,WCF 需要一个完整的服务说明 (ServiceDescription 类) 表示。 ServiceHost类 ServiceDescription 从服务类型和配置信息创建,然后使用该说明为 ChannelDispatcher 说明中的每个终结点创建对象。

使用 ServiceHost 对象加载服务、配置终结点、应用安全设置,并启动侦听器来处理传入的请求。

构造函数

构造函数
ServiceHost()

初始化 ServiceHost 类的新实例。

ServiceHost(Object, Uri[])

使用服务的实例及其指定的基址初始化 ServiceHost 类的新实例。

ServiceHost(Type, Uri[])

使用服务的类型及其指定的基址初始化 ServiceHost 类的新实例。

属性

属性
Authentication

获取服务身份验证行为。

(继承自 ServiceHostBase)
Authorization

获取所承载服务的授权行为。

(继承自 ServiceHostBase)
BaseAddresses

获取所承载服务使用的基址。

(继承自 ServiceHostBase)
ChannelDispatchers

获取服务主机所使用的通道调度程序的集合。

(继承自 ServiceHostBase)
CloseTimeout

获取或设置允许服务主机关闭的时间间隔。

(继承自 ServiceHostBase)
Credentials

获取所承载服务的凭据。

(继承自 ServiceHostBase)
DefaultCloseTimeout

获取允许服务主机关闭的默认时间间隔。

(继承自 ServiceHostBase)
DefaultOpenTimeout

获取允许服务主机打开的默认时间间隔。

(继承自 ServiceHostBase)
Description

获取所承载服务的说明。

(继承自 ServiceHostBase)
Extensions

获取当前指定的服务主机的扩展。

(继承自 ServiceHostBase)
ImplementedContracts

检索由所承载服务实现的协定。

(继承自 ServiceHostBase)
IsDisposed

获取一个值,该值指示通信对象是否已被释放。

(继承自 CommunicationObject)
ManualFlowControlLimit

获取或设置由所承载服务接收的消息的流控制限制。

(继承自 ServiceHostBase)
OpenTimeout

获取或设置允许服务主机打开的时间间隔。

(继承自 ServiceHostBase)
SingletonInstance

获取承载服务的单一实例。

State

获取指示通信对象当前状态的值。

(继承自 CommunicationObject)
ThisLock

获取在状态转换过程中保护类实例的相互排斥锁。

(继承自 CommunicationObject)

方法

方法
Abort()

使通信对象立即从其当前状态转换到正在关闭状态。

(继承自 CommunicationObject)
AddBaseAddress(Uri)

将基址添加到服务主机。

(继承自 ServiceHostBase)
AddDefaultEndpoints()

使用默认绑定为在服务主机中找到的每个协定中的所有基址添加服务终结点。

(继承自 ServiceHostBase)
AddServiceEndpoint(ServiceEndpoint)

将指定的服务终结点添加到承载服务中。

(继承自 ServiceHostBase)
AddServiceEndpoint(String, Binding, String)

使用指定的协定、绑定和终结点地址将服务终结点添加到承载服务中。

(继承自 ServiceHostBase)
AddServiceEndpoint(String, Binding, String, Uri)

使用指定的协定、绑定和终结点地址以及包含服务所侦听地址的 URI,将服务终结点添加到承载服务中。

(继承自 ServiceHostBase)
AddServiceEndpoint(String, Binding, Uri)

使用指定的协定、绑定和包含终结点地址的 URI 将服务终结点添加到承载服务中。

(继承自 ServiceHostBase)
AddServiceEndpoint(String, Binding, Uri, Uri)

使用指定的协定、绑定和包含终结点与所侦听地址的 URI,将服务终结点添加到承载服务。

(继承自 ServiceHostBase)
AddServiceEndpoint(Type, Binding, String)

使用指定的协定、绑定和终结点地址将服务终结点添加到承载服务中。

AddServiceEndpoint(Type, Binding, String, Uri)

使用指定的协定、绑定和终结点地址以及服务在其上侦听的 URI 将服务终结点添加到承载服务中。

AddServiceEndpoint(Type, Binding, Uri)

使用指定的协定、绑定和包含终结点地址的 URI 将服务终结点添加到承载服务中。

AddServiceEndpoint(Type, Binding, Uri, Uri)

使用指定的协定、绑定、包含终结点地址的 URI 以及服务在其上侦听的 URI 将服务终结点添加到承载服务中。

ApplyConfiguration()

从配置文件加载服务说明,并将其应用于正在构造的运行库。

BeginClose(AsyncCallback, Object)

开始一个异步操作以关闭通信对象。

(继承自 CommunicationObject)
BeginClose(TimeSpan, AsyncCallback, Object)

开始一个异步操作以在指定超时内关闭通信对象。

(继承自 CommunicationObject)
BeginOpen(AsyncCallback, Object)

开始一个异步操作以打开通信对象。

(继承自 CommunicationObject)
BeginOpen(TimeSpan, AsyncCallback, Object)

开始一个异步操作以在指定时间间隔内打开通信对象。

(继承自 CommunicationObject)
Close()

使通信对象从其当前状态转换到关闭状态。

(继承自 CommunicationObject)
Close(TimeSpan)

使通信对象在指定时间间隔内从当前状态转换到关闭状态。

(继承自 CommunicationObject)
CreateDescription(IDictionary<String,ContractDescription>)

创建承载服务的说明。

EndClose(IAsyncResult)

完成一个异步操作以关闭通信对象。

(继承自 CommunicationObject)
EndOpen(IAsyncResult)

完成一个异步操作以打开通信对象。

(继承自 CommunicationObject)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Fault()

使通信对象从其当前状态转换到出错状态。

(继承自 CommunicationObject)
GetCommunicationObjectType()

获取通信对象的类型。

(继承自 CommunicationObject)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IncrementManualFlowControlLimit(Int32)

以指定的增量增加对消息到所承载服务的流入率的限制。

(继承自 ServiceHostBase)
InitializeDescription(Object, UriSchemeKeyedCollection)

基于承载服务的实例和指定的基址初始化该承载服务的说明。

InitializeDescription(Type, UriSchemeKeyedCollection)

基于承载服务的类型和指定的基址初始化该承载服务的说明。

InitializeDescription(UriSchemeKeyedCollection)

使用协定和服务说明创建并初始化服务主机。

(继承自 ServiceHostBase)
InitializeRuntime()

初始化服务主机的运行库。

(继承自 ServiceHostBase)
LoadConfigurationSection(ServiceElement)

从承载服务的配置文件加载服务元素。

(继承自 ServiceHostBase)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnAbort()

中止服务。

(继承自 ServiceHostBase)
OnBeginClose(TimeSpan, AsyncCallback, Object)

开始在关闭服务主机时调用的异步操作。

(继承自 ServiceHostBase)
OnBeginOpen(TimeSpan, AsyncCallback, Object)

开始在打开服务主机时调用的异步操作。

(继承自 ServiceHostBase)
OnClose(TimeSpan)

关闭所承载服务,包括它们的通道调度程序以及相关的实例上下文和侦听器。

(继承自 ServiceHostBase)
OnClosed()

当服务主机关闭时,释放所承载的可处置服务。

OnClosing()

在通信对象转换到正在关闭状态的过程中被调用。

(继承自 CommunicationObject)
OnEndClose(IAsyncResult)

完成在关闭服务主机时调用的异步操作。

(继承自 ServiceHostBase)
OnEndOpen(IAsyncResult)

完成在打开服务主机时调用的异步操作。

(继承自 ServiceHostBase)
OnFaulted()

在调用了同步错误操作,从而引起通信对象转换为出错状态的情况下,该方法插入对通信对象的处理。

(继承自 CommunicationObject)
OnOpen(TimeSpan)

打开通道调度程序。

(继承自 ServiceHostBase)
OnOpened()

获取所承载服务的服务凭据、服务身份验证和授权行为。

(继承自 ServiceHostBase)
OnOpening()

在通信对象转换到正在打开状态的过程中被调用。

(继承自 CommunicationObject)
Open()

使通信对象从已创建状态转换到已打开状态。

(继承自 CommunicationObject)
Open(TimeSpan)

使通信对象在指定时间间隔内从已创建状态转换到已打开状态。

(继承自 CommunicationObject)
ReleasePerformanceCounters()

释放所承载服务的服务与通道调度程序性能计数器。

(继承自 ServiceHostBase)
SetEndpointAddress(ServiceEndpoint, String)

将指定终结点的终结点地址设置为指定的地址。

(继承自 ServiceHostBase)
ThrowIfDisposed()

如果通信对象已释放,则引发异常。

(继承自 CommunicationObject)
ThrowIfDisposedOrImmutable()

如果通信对象的 State 属性未设置为 Created 状态,则引发异常。

(继承自 CommunicationObject)
ThrowIfDisposedOrNotOpen()

如果通信对象未处于 Opened 状态,则引发异常。

(继承自 CommunicationObject)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

事件
Closed

当通信对象转换到已关闭状态时发生。

(继承自 CommunicationObject)
Closing

当通信对象转换到正在关闭状态时发生。

(继承自 CommunicationObject)
Faulted

在通信对象转换到出错状态时发生。

(继承自 CommunicationObject)
Opened

当通信对象转换到已打开状态时发生。

(继承自 CommunicationObject)
Opening

当通信对象转换到正在打开状态时发生。

(继承自 CommunicationObject)
UnknownMessageReceived

接收未知消息时发生。

(继承自 ServiceHostBase)

显式接口实现

显式接口实现
IDisposable.Dispose()

关闭服务主机。

(继承自 ServiceHostBase)
原文地址:https://www.cnblogs.com/bruce1992/p/15292849.html