แก้ไข

แชร์ผ่าน


HostingEnvironment Class

Definition

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
Inheritance
HostingEnvironment

Examples

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>

Remarks

Each managed application domain contains a static instance of the HostingEnvironment class, which provides access to application-management functions and application services.

Constructors

HostingEnvironment()

Initializes a new instance of the HostingEnvironment class.

Properties

ApplicationHost

This property supports the ASP.NET infrastructure and is not intended to be used directly from your code.

ApplicationID

Gets the unique identifier of the application.

ApplicationMonitors

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

Gets the Cache instance for the current application.

InClientBuildManager

Gets a value that indicates whether the hosting environment has access to the ASP.NET build system.

InitializationException

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

Gets a value indicating whether the current application domain is being hosted by an ApplicationManager object.

MaxConcurrentRequestsPerCPU

Gets or sets the maximum concurrent requests per CPU.

MaxConcurrentThreadsPerCPU

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.

Methods

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from 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.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
Impersonate()

Impersonates the user represented by the application identity.

Impersonate(IntPtr, String)

Impersonates the user specified by the configuration settings for the specified virtual path, or the specified user token.

Impersonate(IntPtr)

Impersonates the user represented by the specified user token.

IncrementBusyCount()

Increases the count of busy objects in the hosted environment by one.

InitializeLifetimeService()

Gives the HostingEnvironment object an infinite lifetime by preventing a lease from being created.

InitiateShutdown()

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()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
MessageReceived()

This method supports the ASP.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)

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()

Returns a string that represents the current object.

(Inherited from Object)
UnregisterObject(IRegisteredObject)

Removes an object from the list of registered objects for the application.

Events

StopListening

[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.

Applies to