Delen via


CS 2007: Running Pipelines in a Console Application

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!

CS2007_PipelineConsoleApp.zip

Comments

  • Anonymous
    August 08, 2006
    I'm blushing (LOL)

    Jeff

  • Anonymous
    August 08, 2006
    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

  • Anonymous
    August 08, 2006
    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.

  • Anonymous
    August 17, 2006
    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!

  • Anonymous
    August 25, 2006
    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

  • Anonymous
    August 25, 2006
    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?

  • Anonymous
    August 28, 2006
    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();
               
           }

  • Anonymous
    August 29, 2006
    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

  • Anonymous
    August 30, 2006
    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

  • Anonymous
    September 24, 2006
    So far you if you have followed this on going Pipeline posts, you should be very familiar with how to...

  • Anonymous
    November 10, 2006
    So you probably know all about how to change the various limits (such as the maximum Baskets per user

  • Anonymous
    November 11, 2006
    As previously discussed the pipeline system enables sequential workflow processing. It is a COM-based...

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

  • Anonymous
    August 01, 2007
    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

  • Anonymous
    August 28, 2008
    The comment has been removed

  • Anonymous
    August 29, 2008
    Hi PBen, I am sorry but I will be unable to help you with your issue since I am not working on the Commerce Server product anymore. I would suggest posting this question to either the Commerce Server MSDN newsgroup or the Commerce Server Blog (http://blogs.msdn.com/commerce/). Thanks, Nihit

  • Anonymous
    May 12, 2010
    Hello, I tried described how to place orders in a console application using Commerce Server 2009 and the multichannel foundation you can find my post here: http://ecommerceondotnet.blogspot.com/2010/05/using-commerce-server-2009-to-place.html

  • Anonymous
    January 08, 2012
    Step by step configuring Commerce Server 2009 Web Services sharepointtaskmaster.blogspot.com/.../step-by-step-configuring-commerce.html