This documentation is archived and is not being maintained.

ApplicationManager.CreateObject Method (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.

Namespace:  System.Web.Hosting
Assembly:  System.Web (in System.Web.dll)

[SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public IRegisteredObject CreateObject(
	string appId,
	Type type,
	string virtualPath,
	string physicalPath,
	bool failIfExists


Type: System.String

The unique identifier for the application that owns the object.

Type: System.Type

The type of the object to create.

Type: System.String

The virtual path to the application.

Type: System.String

The physical path to the application.

Type: System.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.

Return Value

Type: System.Web.Hosting.IRegisteredObject
A new object of the specified type.


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.

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.

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

  void IRegisteredObject.Stop(bool immediate)
    foreach (SampleComponent c in components)

  public SampleComponent CreateComponent()
    SampleComponent newComponent = new SampleComponent();
    return newComponent;

public class SampleComponent : IRegisteredObject
  void IRegisteredObject.Stop(bool immediate)
    // Clean up component resources here.

  public void Initialize()
    // Initialize component here.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0