How to Create a Custom Commerce Module

What is a Commerce Server Module?

One of Commerce Server's most useful features is the extensibility of the Commerce Server Modules. A Commerce Server module is simply a class that implements the System.Web.IHttpModule interface:

public interface IHttpModule{ void Dispose(); void Init(HttpApplication context);}

 

public abstract class CommerceModule : IHttpModule{// Methodsprotected CommerceModule();protected static void DeclareModuleDependency(Type moduleType, Type dependentType);protected static void DeclareResourceDependency(string resourceName, Type dependentType);public void Dispose();protected virtual void Dispose(bool disposing);public virtual void Init(HttpApplication appInstance);

// Propertiespublic bool IsInitialized { get; }

// Fieldsprivate bool _initialized;}

Does Commerce Server uses CommerceModule?

Yes Commerce Server systems all use CommerceModules, the following is a list of Commerce Servers Modules: 

CommerceCacheModuleCommerceCatgalogModuleCommerceAuthenticationModuleCommerceDataWarehousAuthenticationModuleCommerceDirectMailerAutheticationModuleCommerceOrderModuleCommerceContentSelectionModuleCommerceExpressioModule

These modules are listed in the web.Config and notice the hierarchy? This is due to the dependency of each other. For example the CommerceApplicationModule must be loaded first as other Commerce Server modules depend on it. When creating your own Commerce Modules take care to added it after the module you are depended.

<system.web><httpModules><!-- COMMERCE SERVER HTTP MODULESThese configuration sections are required for the Commerce Server .NET ApplicationFramework to function properly. They must be registered using the strong name for theassemblies.--><add name="CommerceApplication" type="Microsoft.CommerceServer.Runtime.CommerceApplicationModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceAuthentication" type="Microsoft.CommerceServer.Runtime.CommerceAuthenticationModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceOrder" type="Microsoft.CommerceServer.Runtime.Orders.CommerceOrderModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceCatalog" type="Microsoft.CommerceServer.Runtime.Catalog.CommerceCatalogModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceProfile" type="Microsoft.CommerceServer.Runtime.Profiles.CommerceProfileModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceExpressionEvaluator" type="Microsoft.CommerceServer.Runtime.Targeting.CommerceExpressionModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceCache" type="Microsoft.CommerceServer.Runtime.Caching.CommerceCacheModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceContentSelection" type="Microsoft.CommerceServer.Runtime.Targeting.CommerceContentSelectionModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add name="CommerceDataWarehouseAuthenticationModule" type="Microsoft.CommerceServer.Runtime.CommerceDataWarehouseAuthenticationModule, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/></httpModules> ... ... ... ... <system.web>

The HTTP Pipeline

So lets take a step back and explain HttpModules. When a request is received by IIS and is mapped to ASP.NET extension (e.g. aspx), ASP.NET will look for an IHttpHandler implementation in the web.config file associated with that extension. So by default when you create an ASP.NET site and have not modified the web.config ASP.NET creates a handler based on System.Web.UI.Page. So when you create a HttpModule and modified the web.config to hook into the HTTP pipeline, the ASP.NET runtime calls the module's Init and Dispose methods.

To read more about how HTTP modules read the following article HTTP Handlers and HTTP Modules in ASP.NET.

Develop your first Commerce Module

So lets begin and create out first Commerce Module. In this sample our scenario is going to be simple we need to create a module to integrate an Image Management system into Commerce Server.

I have already created a template for Commerce Module under my GotDotNET community. Although the Module was created for Commerce Server 2002 with minimal changes you can convert it to Commerce Server 2007.

Commerce Module is a complete sample that displays dependencies of other Commerce Resources and Modules.

In our sample we are not going to worry about dependency of resources just Commerce Catalog Module.

Developers Start Your Engines

Roll up your sleeves and let's get down and dirty:

  1. Make sure that you have downloaded the Commerce Module template.

  2. Open the CommerceModuleTemplate.sln and when Visual Studio asks you to convert it follow the default steps.

  3. Make sure that the Microsoft.CommerceServer.Runtime is accurate version 6.0.1.0.

  4. Add a using statement for use of CommerceCatalogModule
    using Microsoft.CommerceServer.Runtime.Catalog;

  5. In the Init method modify the dependency to CommerceCatalogModule and since we do not depend or need to create a resource do not create a dependency for a resource.
     

    /// <summary>/// CommerceModule.Init implementation/// </summary>/// <param name="appInstance"></param>public override void Init(HttpApplication appInstance){    // First declare module and resource dependencies using the    // CommerceModule protected helper methods. These will throw    // CommerceResourceDependencyExceptions or    // CommerceModuleDependencyExceptions if the dependencies don’t exist    // This module depends on the CatalogModule having been initialized    // in the pipeline before this module.    DeclareModuleDependency(typeof(CommerceCatalogModule), this.GetType());        // Create the singleton instance of the resource        if (templateresource == null)        {            // Lock to prevent race conditions and ensure that exactly one            // instance of the resource is created            lock (typeof(TemplateContext))            {                if (templateresource == null)                    templateresource = CreateResource();            }        }

        // subscribe to BeginRequest events    appInstance.BeginRequest += new EventHandler(OnBeginRequest); }

     
  6. Make sure to find replace the word template with something more meaningful for your application.

  7. Modify the web.config

    <httpModules> <add type="type" name="name" /> </httpModules>

  8. So now we have a Commerce Module that can be accessed in your aspx page using the following code:

    HttpContext.Current["<yourModuleKey>"].SomeMethod();

Summary

I wanted to get this out before the last post of our Image Management so you had a better understanding of Commerce Server Modules and creating your own custom Commerce Modules.