Share via


How to Create an InventoryContext Object

The InventoryContext class provides the functionality to manage inventory catalogs. The InventoryContext object cannot be created directly. Instead, create a CatalogContext object and get the InventoryContext object from the InventoryContext property of the CatalogContext object.

The InventoryContext object can be obtained in two modes, depending on how the CatalogContext object was initialized. It can be in local mode, where the code accesses the Commerce Server Core Systems objects and the database directly and does not use a Web service.

Alternatively, it can be in agent mode, where calls to the catalog system are made through the Web service with the help of an agent.

For more information about the programming modes, see Understanding the Different Types of Commerce Server APIs.

For best performance, do not create multiple InventoryContext objects. Instead, create a single instance and reuse it for the lifetime of the application.

To create an InventoryContext object in local mode

  1. Create a CatalogSiteAgent object. This object contains the configuration information that is required to connect to the catalog database.

  2. Create a CatalogContext object.

  3. Get the InventoryContext object from the InventoryContext property of the CatalogContext object. The InventoryContext object contains the methods that you need to manage inventory catalogs.

To create an InventoryContext object in agent mode

  1. Create a CatalogServiceAgent object. This object contains the authentication information that is required to communicate with a Web service.

  2. Create a CatalogContext object.

  3. Get the InventoryContext object from the InventoryContext property of the CatalogContext object. The InventoryContext object contains the methods that you need to manage inventory catalogs.

Example

This example creates two InventoryContext objects. The first method uses a CatalogSiteAgent object to create the CatalogContext object in local mode. The second method uses a CatalogServiceAgent object to create the CatalogContext object in agent mode. It uses a parameter url, which is the URL of the Web service against which the agent will run. And this is a test CatalogProperty.

public static InventoryContext CreateInventoryContextFromSiteAgent()
{
    // Create a CatalogSiteAgent to connect to the database.
    CatalogSiteAgent catalogSiteAgent = new CatalogSiteAgent();
    catalogSiteAgent.SiteName = "StarterSite";
    catalogSiteAgent.IgnoreInventorySystem = false;
    
    // Create a CatalogContext object.
    CatalogContext catalogContext = CatalogContext.Create(catalogSiteAgent);
    // Get the InventoryContext property.
    InventoryContext inventoryContext =  catalogContext.InventoryContext;

    return inventoryContext;
}

public static InventoryContext CreateInventoryContextFromServiceAgent(string url)
{
    // Create a CatalogServiceAgent to connect to the Web service.
    CatalogServiceAgent serviceAgent = new CatalogServiceAgent(url, new string[] {"ntlm", "kerberos", negotiate"});
    // Create a CatalogContext object.
    CatalogContext catalogContext = CatalogContext.Create(serviceAgent);
    // Get the InventoryContext property.
    InventoryContext inventoryContext = catalogContext.InventoryContext;

    return inventoryContext;
}

See Also

Other Resources

Before You Start Developing with the Catalog System

Concepts of the Catalog System