ApplicationManager.CreateObject Method

Definition

Creates an object for the specified application domain and object type.

Overloads

CreateObject(IApplicationHost, Type)

Creates an object for the specified application domain, based on type.

CreateObject(String, Type, String, String, Boolean)

Creates an object for the specified application domain based on type, virtual and physical paths, and a Boolean value indicating failure behavior when an object of the specified type already exists.

CreateObject(String, Type, String, String, Boolean, Boolean)

Creates an object for the specified application domain based on type, virtual and physical paths, a Boolean value indicating failure behavior when an object of the specified type already exists, and a Boolean value indicating whether hosting initialization error exceptions are thrown.

CreateObject(IApplicationHost, Type)

Creates an object for the specified application domain, based on type.

public:
 System::Web::Hosting::IRegisteredObject ^ CreateObject(System::Web::Hosting::IApplicationHost ^ appHost, Type ^ type);
public System.Web.Hosting.IRegisteredObject CreateObject (System.Web.Hosting.IApplicationHost appHost, Type type);
member this.CreateObject : System.Web.Hosting.IApplicationHost * Type -> System.Web.Hosting.IRegisteredObject
Public Function CreateObject (appHost As IApplicationHost, type As Type) As IRegisteredObject

Parameters

appHost
IApplicationHost

An IApplicationHost object.

type
Type

The type of the object to create.

Returns

A new object of the type specified in type.

Exceptions

A physical path for the application does not exist.

appHost is null.

-or-

type is null.

Remarks

CreateObject is introduced in the .NET Framework version 3.5. For more information, see Versions and Dependencies.

Applies to

CreateObject(String, Type, String, String, Boolean)

Creates an object for the specified application domain based on type, virtual and physical paths, and a Boolean value indicating failure behavior when an object of the specified type already exists.

public:
 System::Web::Hosting::IRegisteredObject ^ CreateObject(System::String ^ appId, Type ^ type, System::String ^ virtualPath, System::String ^ physicalPath, bool failIfExists);
public System.Web.Hosting.IRegisteredObject CreateObject (string appId, Type type, string virtualPath, string physicalPath, bool failIfExists);
member this.CreateObject : string * Type * string * string * bool -> System.Web.Hosting.IRegisteredObject
Public Function CreateObject (appId As String, type As Type, virtualPath As String, physicalPath As String, failIfExists As Boolean) As IRegisteredObject

Parameters

appId
String

The unique identifier for the application that owns the object.

type
Type

The type of the object to create.

virtualPath
String

The virtual path to the application.

physicalPath
String

The physical path to the application.

failIfExists
Boolean

true to throw an exception if an object of the specified type is currently registered; false to return the existing registered object of the specified type.

Returns

A new object of the specified type.

Exceptions

physicalPath is null

-or-

physicalPath is not a valid application path.

-or-

type does not implement the IRegisteredObject interface.

appID is null.

-or-

type is null.

failIfExists is true and an object of the specified type is already registered.

Examples

The following code example is an implementation of the object-factory design pattern for registered objects. Using the factory pattern enables you to register multiple instances of an object. The example contains two objects: SampleComponent, which is the object the application will use multiple instances of, and SampleComponentFactory, which manages a list of SampleComponent instances.

using System.Web.Hosting;

public class SampleComponentFactory : IRegisteredObject
{
  private ArrayList components = new ArrayList();

  public void Start()
  {
    HostingEnvironment.RegisterObject(this);
  }

  void IRegisteredObject.Stop(bool immediate)
  {
    foreach (SampleComponent c in components)
    {
      ((IRegisteredObject)c).Stop(immediate);
    }
    HostingEnvironment.UnregisterObject(this);
  }

  public SampleComponent CreateComponent()
  {
    SampleComponent newComponent = new SampleComponent();
    newComponent.Initialize();
    components.Add(newComponent);
    return newComponent;
  }
}

public class SampleComponent : IRegisteredObject
{
  void IRegisteredObject.Stop(bool immediate)
  {
    // Clean up component resources here.
  }
  
  public void Initialize()
  {
    // Initialize component here.
  }
}
Imports System.Web.Hosting

Public Class SampleComponentFactory
  Implements IRegisteredObject

  Dim components As ArrayList = New ArrayList()

  Public Sub Start()
    HostingEnvironment.RegisterObject(Me)
  End Sub

  Public Sub [Stop](ByVal immediate As Boolean) Implements System.Web.Hosting.IRegisteredObject.Stop
    For Each c As SampleComponent In components
      CType(c, IRegisteredObject).Stop(immediate)
    Next
    HostingEnvironment.UnregisterObject(Me)
  End Sub

  Public Function CreateComponent() As SampleComponent
    Dim newComponent As SampleComponent
    newComponent = New SampleComponent
    newComponent.Initialize()

    components.Add(newComponent)
    Return newComponent
  End Function
End Class

Public Class SampleComponent
  Implements IRegisteredObject


  Sub [Stop](ByVal immediate As Boolean) Implements System.Web.Hosting.IRegisteredObject.Stop
    ' Clean up component resources here.
  End Sub

  Public Sub Initialize()
    ' Initialize component here.
  End Sub
End Class

Remarks

The CreateObject method is used to create and register objects in the application. Only one object of each type can be created. If you need to create multiple objects of the same type, you must implement an object factory. For more information, see the code example in this topic.

Each application, identified by a unique application identifier, runs in its own application domain. The CreateObject method creates an object of the specified type in the application domain of the application specified in the appID parameter. If an application domain does not exist for the specified application, one is created before the object is created.

The failIfExists parameter controls the behavior of the CreateObject method when an object of the specified type already exists in the application. When failIfExists is true, the CreateObject method throws an InvalidOperationException exception.

When failIfExists is false, the CreateObject method returns the existing registered object of the specified type.

The CreateObject method calls the overload that takes an additional throwOnError parameter with throwOnError set to false.

Applies to

CreateObject(String, Type, String, String, Boolean, Boolean)

Creates an object for the specified application domain based on type, virtual and physical paths, a Boolean value indicating failure behavior when an object of the specified type already exists, and a Boolean value indicating whether hosting initialization error exceptions are thrown.

public:
 System::Web::Hosting::IRegisteredObject ^ CreateObject(System::String ^ appId, Type ^ type, System::String ^ virtualPath, System::String ^ physicalPath, bool failIfExists, bool throwOnError);
public System.Web.Hosting.IRegisteredObject CreateObject (string appId, Type type, string virtualPath, string physicalPath, bool failIfExists, bool throwOnError);
member this.CreateObject : string * Type * string * string * bool * bool -> System.Web.Hosting.IRegisteredObject
Public Function CreateObject (appId As String, type As Type, virtualPath As String, physicalPath As String, failIfExists As Boolean, throwOnError As Boolean) As IRegisteredObject

Parameters

appId
String

The unique identifier for the application that owns the object.

type
Type

The type of the object to create.

virtualPath
String

The virtual path to the application.

physicalPath
String

The physical path to the application.

failIfExists
Boolean

true to throw an exception if an object of the specified type is currently registered; false to return the existing registered object of the specified type.

throwOnError
Boolean

true to throw exceptions for hosting initialization errors; false to not throw hosting initialization exceptions.

Returns

A new object of the specified type.

Exceptions

physicalPath is null

-or-

physicalPath is not a valid application path.

-or-

type does not implement the IRegisteredObject interface.

appID is null.

-or-

type is null.

failIfExists is true and an object of the specified type is already registered.

Remarks

This overload of the CreateObject method provides the throwOnError parameter, which allows you to control whether hosting initialization exceptions are thrown. The overload of the CreateObject method that does not provide throwOnError calls this overload with the parameter set to false.

Applies to