This topic has not yet been rated - Rate this topic

HostingEnvironment Class

Provides application-management functions and application services to a managed application within its application domain. This class cannot be inherited.

System.Object
  System.MarshalByRefObject
    System.Web.Hosting.HostingEnvironment

Namespace:  System.Web.Hosting
Assembly:  System.Web (in System.Web.dll)
public sealed class HostingEnvironment : MarshalByRefObject

The HostingEnvironment type exposes the following members.

  Name Description
Public method HostingEnvironment Infrastructure. Initializes a new instance of the HostingEnvironment class.
Top
  Name Description
Public property Static member ApplicationHost This property supports the ASP.NET infrastructure and is not intended to be used directly from your code.
Public property Static member ApplicationID Gets the unique identifier of the application.
Public property Static member ApplicationPhysicalPath Gets the physical path on disk to the application's directory.
Public property Static member ApplicationVirtualPath Gets the root virtual path of the application.
Public property Static member Cache Gets the Cache instance for the current application.
Public property Static member InClientBuildManager Gets a value that indicates whether the hosting environment has access to the ASP.NET build system.
Public property Static member InitializationException Gets any exception thrown during initialization of the HostingEnvironment object.
Public property Static member IsHosted Gets a value indicating whether the current application domain is being hosted by an ApplicationManager object.
Public property Static member MaxConcurrentRequestsPerCPU Gets or sets the maximum concurrent requests per CPU.
Public property Static member MaxConcurrentThreadsPerCPU Gets or sets the maximum concurrent threads per CPU.
Public property Static member ShutdownReason Returns an enumerated value that indicates why the application terminated.
Public property Static member SiteName Gets the name of the site.
Public property Static member VirtualPathProvider Gets the virtual path provider for this application.
Top
  Name Description
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Static member DecrementBusyCount Reduces the count of busy objects in the hosted environment by one.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Static member Impersonate() Impersonates the user represented by the application identity.
Public method Static member Impersonate(IntPtr) Impersonates the user represented by the specified user token.
Public method Static member Impersonate(IntPtr, String) Impersonates the user specified by the configuration settings for the specified virtual path, or the specified user token.
Public method Static member IncrementBusyCount Increases the count of busy objects in the hosted environment by one.
Public method InitializeLifetimeService Infrastructure. Gives the HostingEnvironment object an infinite lifetime by preventing a lease from being created. (Overrides MarshalByRefObject.InitializeLifetimeService().)
Public method Static member InitiateShutdown Starts shutting down the Web application associated with this host and removes registered objects from the system.
Public method Static member MapPath Maps a virtual path to a physical path on the server.
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method Static member MessageReceived This method supports the ASP.NET infrastructure and is not intended to be used directly from your code.
Public method Static member RegisterObject Places an object in the list of registered objects for the application.
Public method Static member RegisterVirtualPathProvider Registers a new VirtualPathProvider instance with the ASP.NET compilation system.
Public method Static member SetCultures() Sets the current thread to the culture specified in the application configuration file.
Public method Static member SetCultures(String) Sets the current thread to the culture of the specified virtual path.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Static member UnregisterObject Removes an object from the list of registered objects for the application.
Top

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

The following code example is a Web page 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>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ