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

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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(string appId, Type type, string virtualPath, string physicalPath, bool failIfExists);

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

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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(string appId, Type type, string virtualPath, string physicalPath, bool failIfExists, bool throwOnError);

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1