CS 2007: Running Pipelines in a Console Application

CS 2007: Running Pipelines in a Console Application

Rate This
  • Comments 18

One of things you might need to do occasionally is to run pipelines from code which is not running in a web context (i.e. the code is not part of a web site or web service). This could be a console app, a WinForm app or even a Windows service. The reason this is a non-trivial task is firstly because this is not a core scenario targeted by the Commerce Server platform and secondly because when you run a web app, a lot of the work required to run pipelines is done behind the scenes when the various Commerce related sections are parsed in the web.config at app startup time. The good news though is that it is possible to run pipelines in a console (or WinForm or Windows service etc. etc. you get the idea) app, thus making it easier for you to test your components or target a niche scenario important for your business.

I have attached a fully functional VS 2005 solution that illustrates how you can run pipelines in a non-web environment. Hopefully this sample should help you get started – though please keep in mind that this is purely for illustrating the core scenario of configuring and running pipelines in a console app and is not meant to showcase the design. Also please keep in mind that the use of attached code samples are subject to the terms specified here. Hope you find the sample code useful.

On a related note, there’s been a lot happening on the CS 2007 front. In case you haven't heard, we have decided to give away our Developer Edition for FREE - that's right - zero cost to you to get started with developing a full-fledged Enterprise level eCommerce site on the Commerce Server platform. We also announced the General Availability (GA) of Commerce Server 2007 to the all partners and customers and on the documentation front, launched the TechNet site for Commerce Server and have also made the CS 2007 Migration Guide available as separate download, which should help you learn all about migrating from earlier versions of Commerce Server to CS 2007.

Also, we have some web casts available on-demand on the TechNet site which also contains an article by Jeff Lynch on Integrating Commerce Server Orders with BizTalk Adapters. Kudos Jeff!

Attachment: CS2007_PipelineConsoleApp.zip
Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
  • I'm blushing (LOL)

    Jeff
  • Thanks for that, Nihit.

    On a similar(ish) subject, is there any way I can get a CommerceContext to initialise in a unit test project? I'm struggling to test such things because CommerceContext.Current always returns null when I'm running my unit tests.


    Regards,
    Paul
  • Hi Paul,

    Unfortunately, it is not possible to create a CommerceContext in a non-web environment. The only way is to manually create the various objects hanging off of the CommerceContext, such as the Pipelines collection, the OrderContext etc. and then to use them invidually (or you could wrap them in a class which would be like CommerceContextForConsoleApps). The sample attached will get you a lot of the things hanging off of CommerceContext in a console environment.
  • Now that the Commerce Server development team has shipped their flagship product, they've started to publish some great new posts about using Commerce Server 2007. If you don't already subscribe to these blogs, you really should!
  • Hi

    Do you have an example on how I could do the same for the targetting system? I'm wrapping a lot of the Commerce systems and the TargetingSystem seems to be the last frontier.

    I notice that you do add some configuration information for the marketing system, but I don't see how I can instantiate my own TargettingSystemInfo instance.

    Please help
  • Hi Phil,

    I must ask as to what you are trying to do withte TargetingSystemInfo class? Is it not possible to do that from the DataManagement side?
    I don't think you can directly create an instance of this class since the constructor is internal and only called by CommerceContext.

    However, the class does not have anything special in it:
    A reference to an Expression Evaluator
    A reference to a TargetingContext profile
    A reference to the ContentSelectionContexts

    You can maintain these references from a class of your own and then you don’t need to use TargetingSystemInfo.

    Would that work for you?
  • I don't mind wrapping it up.

    I need to do something like

    ContentSelector cso = null;
               
               cso = CommerceContext.Current.TargetingSystem.SelectionContexts["advertising"].GetSelector();
           

    Would the code below work to properly instantiate cso ?

    ContentSelectionPipeline advertisingPipeline = new ContentSelectionPipeline("advertising", GetPipelinePath("advertising.pcf"), true, "advertising.log");
    ContentSelectionPipeline discountsPipeline = new ContentSelectionPipeline("discounts", GetPipelinePath("discounts.pcf"), true, "discount.log");
               ContentSelectionPipeline eventPipeline = new ContentSelectionPipeline("recordevent", GetPipelinePath("RecordEvent.pcf"), true, "recordEvent.log");
               string redirectUrl = "AdRedirect.aspx";
               DebugContext debugContext = new ConsoleDebugContext(DebugMode.Debug);

               CommerceResourceCollection resources = new CommerceResourceCollection(siteName);
               CommerceResource marketingResource = resources["Marketing"];

               string marketingConnStr = marketingResource["connstr_db_marketing"].ToString();

               string cleanedMarketingConnStr;
               CleanSqlClientConnectionString(marketingConnStr, out cleanedMarketingConnStr);

               Microsoft.CommerceServer.Runtime.Targeting.ExpressionEvaluator evaluator = new Microsoft.CommerceServer.Runtime.Targeting.ExpressionEvaluator(cleanedMarketingConnStr, debugContext);

               ContentSelectionContext advertising_context = new ContentSelectionContext("advertising", advertisingPipeline, eventPipeline, "Advertising", redirectUrl, evaluator, debugContext);
               ContentSelectionContext discounts_context = new ContentSelectionContext("discounts", discountsPipeline, eventPipeline, "Discounts", redirectUrl, evaluator, debugContext);

               ContentSelectionContextCollection selections = new ContentSelectionContextCollection();
               selections.Add("advertising", advertising_context);
               selections.Add("discounts", discounts_context);

               Microsoft.CommerceServer.Runtime.Targeting.ContentSelector cso = selections["advertising"].GetSelector();
               
           }

  • Yes, I could use that option to properly wrap the information.

    I think I can do all the instantiations correctly except for the evaluator. I'm not sure I should be using the Marketing SQL string (as per your code) and I'm not sure which DebugContext to feed it. I could use a simple ConsoleDebugContext, but I'm not sure if it's ok.

    Do you have an example that shows how to instantiate the Evaluator?

    Thanks
  • Hi Phil,

    The ConsoleDebugContext should be fine to use in this scenario. As for the SQL connection string - are you getting an error when using that? I would suggest trying some different formats to see if the Evaluator is expecting it in a particular format.

    Thanks,
    Nihit
  • So far you if you have followed this on going Pipeline posts, you should be very familiar with how to...
  • So you probably know all about how to change the various limits (such as the maximum Baskets per user

  • As previously discussed the pipeline system enables sequential workflow processing. It is a COM-based...

  • As I spoke about in an earlier post, Commerce Server 2007 leverages a COM-based pipeline system...

  • I successfully run your example, but after

    PurchaseOrder order = cart.SaveAsOrder();

    , the application stops (without exception), whith code 2131666259 (0x7f0ea553). Anyway, the purchaseOrder is successfully created.

    Any idea ?

    Thanks,

    Olivier

  • Hi,

    I am using the code provided above for some testing.  

    Now, I am having some problems with the payment system, since when running the code the payment  is not being affected.  Most probably it is due to the fact that the CreditCardPayment object being created is not being used.  In fact no new entry is being added in the table transactions.dbo.CreditCardPayments.

    Now, I tried to modify the code by adding the following code:

    basket.OrderForms[0].Payments.Add(ccPmt);

    When I execute the application, the following error is being given:

    "Execution of pipeline 'CHECKOUT' failed."

    And the inner exception being:

    {"Component Execution failed for component[0x0]  hr: 0x80070057\r\nProgID: Commerce.PaymentMethodRouter\r\nPipelineCollection in the context dictionary does not contain 'creditcard' pipeline. Make sure creditcard pipeline is configured in the configuration file."}

    It must be noted that the creditcard pipeline is available in the pipelines folder.

    What should the problem be?  If there is the need of a configuration file, how should this configuration be configured for a console application?

    Thanks in Advance

Page 1 of 2 (18 items) 12