HostingEnvironment 类

定义

在托管应用程序的应用程序域内向托管应用程序提供应用程序管理功能和应用程序服务。Provides application-management functions and application services to a managed application within its application domain. 无法继承此类。This class cannot be inherited.

public ref class HostingEnvironment sealed : MarshalByRefObject
public sealed class HostingEnvironment : MarshalByRefObject
type HostingEnvironment = class
    inherit MarshalByRefObject
Public NotInheritable Class HostingEnvironment
Inherits MarshalByRefObject
继承
HostingEnvironment

示例

下面的代码示例是一个网页,该网页显示 HostingEnvironment 对象中可用的应用程序信息。The following code example is a webpage that displays the application information available from the HostingEnvironment object.

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Hosting" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    appID.Text = HostingEnvironment.ApplicationID;
    appPPath.Text = HostingEnvironment.ApplicationPhysicalPath;
    appVPath.Text = HostingEnvironment.ApplicationVirtualPath;
    siteName.Text = HostingEnvironment.SiteName;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Hosting Environment Sample</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <table>
        <tr>
          <td colspan="2">
            <b>HostingEnvironment Properties</b></td>
        </tr>
        <tr>
          <td>
            Application ID:
          </td>
          <td>
            <asp:Label ID="appID" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            Application Physical Path:
          </td>
          <td>
            <asp:Label ID="appPPath" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            Application Virtual Path:
          </td>
          <td>
            <asp:Label ID="appVPath" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            Site Name:
          </td>
          <td>
            <asp:Label ID="siteName" runat="server" />
          </td>
        </tr>
      </table>
    </div>
  </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Hosting" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    appID.Text = HostingEnvironment.ApplicationID
    appPPath.Text = HostingEnvironment.ApplicationPhysicalPath
    appVPath.Text = HostingEnvironment.ApplicationVirtualPath
    siteName.Text = HostingEnvironment.SiteName
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>Hosting Environment Sample</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <table>
        <tr>
          <td colspan="2">
            <b>HostingEnvironment Properties</b></td>
        </tr>
        <tr>
          <td>
            Application ID:
          </td>
          <td>
            <asp:Label ID="appID" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            Application Physical Path:
          </td>
          <td>
            <asp:Label ID="appPPath" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            Application Virtual Path:
          </td>
          <td>
            <asp:Label ID="appVPath" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            Site Name:
          </td>
          <td>
            <asp:Label ID="siteName" runat="server" />
          </td>
        </tr>
      </table>
    </div>
  </form>
</body>
</html>

注解

每个托管应用程序域都包含一个 HostingEnvironment 类的静态实例,该实例提供对应用程序管理功能和应用程序服务的访问。Each managed application domain contains a static instance of the HostingEnvironment class, which provides access to application-management functions and application services.

构造函数

HostingEnvironment()

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

属性

ApplicationHost

此属性支持 ASP.NETASP.NET 基础结构,不能在代码中直接使用。This property supports the ASP.NETASP.NET infrastructure and is not intended to be used directly from your code.

ApplicationID

获取应用程序的唯一标识符。Gets the unique identifier of the application.

ApplicationMonitors

获取 ASP.NET 子系统用于维护应用程序运行状况的一组可替换监视器对象。Gets a group of replaceable monitor objects used by ASP.NET subsystems to maintain application health.

ApplicationPhysicalPath

获取磁盘上指向应用程序目录的物理路径。Gets the physical path on disk to the application's directory.

ApplicationVirtualPath

获取应用程序的根虚拟路径。Gets the root virtual path of the application.

Cache

获取当前应用程序的 Cache 实例。Gets the Cache instance for the current application.

InClientBuildManager

获取一个值,该值指示宿主环境是否具有对 ASP.NET 生成系统的访问权。Gets a value that indicates whether the hosting environment has access to the ASP.NET build system.

InitializationException

获取在 HostingEnvironment 对象初始化期间引发的任何异常。Gets any exception thrown during initialization of the HostingEnvironment object.

IsDevelopmentEnvironment

获取一个值,该值指示当前应用程序是否处于开发环境。Gets a value that indicates whether the current application is in a development environment.

IsHosted

获取一个值,该值指示当前应用程序域是否在由 ApplicationManager 对象承载。Gets a value indicating whether the current application domain is being hosted by an ApplicationManager object.

MaxConcurrentRequestsPerCPU

获取或设置每 CPU 的最大并发请求数。Gets or sets the maximum concurrent requests per CPU.

MaxConcurrentThreadsPerCPU

获取或设置每 CPU 的最大并发线程数。Gets or sets the maximum concurrent threads per CPU.

ShutdownReason

返回指示应用程序终止原因的枚举值。Returns an enumerated value that indicates why the application terminated.

SiteName

获取站点的名称。Gets the name of the site.

VirtualPathProvider

获取此应用程序的虚拟路径提供程序。Gets the virtual path provider for this application.

方法

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
DecrementBusyCount()

将宿主环境中繁忙对象的计数减一。Reduces the count of busy objects in the hosted environment by one.

Equals(Object)

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

(继承自 Object)
GetHashCode()

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

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

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

(继承自 Object)
Impersonate()

模拟应用程序标识所表示的用户。Impersonates the user represented by the application identity.

Impersonate(IntPtr)

模拟指定用户标记表示的用户。Impersonates the user represented by the specified user token.

Impersonate(IntPtr, String)

模拟由指定虚拟路径的配置设置或由指定用户标记所指定的用户。Impersonates the user specified by the configuration settings for the specified virtual path, or the specified user token.

IncrementBusyCount()

将宿主环境中繁忙对象的计数加一。Increases the count of busy objects in the hosted environment by one.

InitializeLifetimeService()

通过阻止创建租约给予 HostingEnvironment 对象无限的生存期。Gives the HostingEnvironment object an infinite lifetime by preventing a lease from being created.

InitiateShutdown()

开始关闭与此宿主关联的 Web 应用程序,并从系统中移除注册对象。Starts shutting down the web application associated with this host and removes registered objects from the system.

MapPath(String)

将虚拟路径映射到服务器上的物理路径。Maps a virtual path to a physical path on the server.

MemberwiseClone()

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

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
MessageReceived()

此方法支持 ASP.NETASP.NET 基础结构,不能在代码中直接使用。This method supports the ASP.NETASP.NET infrastructure and is not intended to be used directly from your code.

QueueBackgroundWorkItem(Action<CancellationToken>)

计划可以在后台运行(与任何请求无关)的任务。Schedules a task which can run in the background, independent of any request.

QueueBackgroundWorkItem(Func<CancellationToken,Task>)

计划可以在后台运行(与任何请求无关)的任务。Schedules a task which can run in the background, independent of any request.

RegisterObject(IRegisteredObject)

将一个对象放在应用程序的已注册对象列表中。Places an object in the list of registered objects for the application.

RegisterVirtualPathProvider(VirtualPathProvider)

在 ASP.NET 编译系统中注册新的 VirtualPathProvider 实例。Registers a new VirtualPathProvider instance with the ASP.NET compilation system.

SetCultures()

将当前线程的区域性设置为应用程序配置文件中指定的区域性。Sets the current thread to the culture specified in the application configuration file.

SetCultures(String)

将当前线程的区域性设置为指定虚拟路径的区域性。Sets the current thread to the culture of the specified virtual path.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
UnregisterObject(IRegisteredObject)

从应用程序的已注册对象列表中移除一个对象。Removes an object from the list of registered objects for the application.

事件

StopListening

[仅在 .NET Framework 4.5.1 中受支持][Supported in the .NET Framework 4.5.1 only]

当辅助进程或与该主机相关联的应用程序池已停止侦听新的请求,并将最终关闭时才发生。Occurs when the worker process or application pool associated with this host has stopped listening for new requests and will eventually shut down.

适用于