Monday 16 April 2012

Adding extensions to a hosted workflow

When hosting Workflows using WorkflowServiceHost or Windows Server AppFabric you may wish to add extensions to the runtime environment so that custom activities can access these extensions. Typically you’ll write custom activities to access some form of extension using a custom interface, and then provide an implementation of this interface at runtime.

When you are self hosting (using WorkflowInvoker or WorkflowApplication) it’s a simple matter of adding an object to the ‘host’, such as shown in the code below…

WorkflowInvoker invoker = new WorkflowInvoker(GetWorkflow());
invoker.Extensions.Add<IActivityLogger>(() => new ActivityLogger());

If you’re hosting using configuration using WorkflowServiceHost or AppFabric however it’s common to define everything in the web.config, however Workflow 4 doesn’t come with an out of the box mechanism to do this.


What I’d like to do is be able to configure up a bunch of extensions using the following in my web.config file…


<workflowExtensions>
<
extensions
>
<
add name="{something}"
extensionType="{Fully Qualifier Typename}"
{Optional Parameters}
/>
</
extensions
>
</
workflowExtensions
>

Now, if you’ve been using .NET for a while you might recognise this pattern – it’s very simple to the way you configure providers in ASP.NET, for things like membership, roles etc. I’ve done this deliberately – the provider model is a good pattern to use and fits this example well.


What I then need is something to read the set of extensions, and instantiate them for each hosted workflow. To do this you need a bit of WCF knowledge.


Behavior Extensions


A behavior extension allows us to inject some code into the WCF pipeline, and in this case I’m using one to instantiate objects defined in my <workflowExtensions> element. First up you need a class that derives from BehaviorExtensionElement as shown in the code below…


/// <summary>
///
Class which is used to define the configuration element for the
/// web.config, so we can add extensions to the workflow
/// </summary>
public class WorkflowExtensionsElement :
BehaviorExtensionElement
{
/// <summary>
///
Return the type of object that is created by this extension
/// </summary>
public override Type BehaviorType
{
get { return typeof(WorkflowExtensionsBehavior); }
}

/// <summary>
///
Construct the behavior
/// </summary>
/// <returns>
A behavior, created from the config in the web.config
</returns>
protected override object CreateBehavior()
{
List<WorkflowExtension> extensions = new List<WorkflowExtension>();

foreach (WorkflowExtensionConfigElement item in WorkflowExtensions)
extensions.Add(new WorkflowExtension(item.Name,
item.ExtensionType, item.Parameters));

return new WorkflowExtensionsBehavior(extensions);
}

///
Declare the extensions collection property.
[ConfigurationProperty("extensions", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(WorkflowExtensionsCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public WorkflowExtensionsCollection WorkflowExtensions
{
get { return (WorkflowExtensionsCollection)base["extensions"]; }
}

}

This uses some other classes that are in the code download that read the appropriate information from the .config file. You then need to include some modifications to the .config file in order for this new behavior to be executed when reading the .config file…

  <system.serviceModel>

<
extensions
>
<behaviorExtensions
>
<
add name="workflowExtensions"
type="MSADC.Workflow.WorkflowExtensionsElement, MSADC.Workflow"
/>
</
behaviorExtensions
>
</
extensions>

This includes the extension such that we can then alter our behaviors element to include the list of extensions…

    <behaviors>
<
serviceBehaviors
>
<
behavior name=""
>
<
serviceMetadata httpGetEnabled="true"
/>
<
serviceDebug includeExceptionDetailInFaults="false"
/>
<
workflowExtensions
>
<
extensions
>
<
add name="logger"
extensionType="Demo.ActivityLogger, Demo"
/>
</
extensions
>
</
workflowExtensions
>

Extension Parameters


Now we have the ability to configure up an extension, how about additional parameters that the extension might need to use? Simple – the code that parses the <add> elements reads any additional attributes and pops these into a NameValueCollection, then during instantiation of the extension I check if the extension derives from the ProviderBase class. If so I simply call the Initialize() method, passing in the name of the provider and the NameValueCollection.


You could use this to pass in a database connection string name or other information – you just need to derive from ProviderBase and read the configuration parameters from the passed collection.


The Code


The download includes two projects, MSADC.Workflow and MSADC.Workflow.Common. I’ve refactored the code from my Hosting Dynamic Workflows example to move this into these libraries as there’s more to come and I need to keep this all in a sensible place. If you’re wondering about the name, MSADC is the shortened form of my company name (MS Application Development Consulting Ltd). I provide bespoke, deeply technical consulting on Windows Workflow 4 (and SIlverlight 4/5, WPF 4, WCF and lots of other parts of the .NET framework).


If you want some help from someone who knows what they’re talking about please get in touch!


The code is available as a NuGet package. Please click here to access the package. If you have any comments please contact me using the Contact Owners link from within NuGet.

20 comments:

de said...

I have read your blog its very attractive and impressive. I like it your blog.

Windows Workflow Foundation Training

Oliver Maurice said...

I think that you need to check out this blog article on compare and contrast essay topics to know how to start and how to write the best compare and contrast essay in your life. Good luck

Assignment Help said...

We are really grateful for your blog post. Nice Blog! Experience the Best Assignment Writing Services at Psychology Essay Help

Florahmelda said...

Our Creative Essay Writing Service is accessible online via a majority of mobile devices to bring most Affordable Creative Essay Papers close to you. Students can access Online Creative Essay For Sale from any location if they have a pertinent mobile device.

Assignment Help said...

Great Blog with useful information. student Faces many problems while doing Assignment.
computer network assignment help

Assignment Help said...

Complete Your Assignment From
Psychology Essay Help

Live Web Tutors said...

Look out for the best assignment, essay, thesis writing services in Canada.


Online Assignment help canada
Online Essay help canada
Online Dissertation help
Homework help Canada
Online Coursework help
Thesis help canada

KeiraDoltan said...

Complete Your Assignment From
Assignment Help Online

mahrez26 said...

สิ่งของนำโชคเรียกทรัพย์ เสริมดวง แต่ละราศี ปี2564 บทความจาก AMBBET เข้าชมเว็บ หรือ อ่านบทความฉบับเต็มๆ ได้แบบฟรี ๆเลย เพราะว่า การเสริมดวง มาเพื่อการเดิมพัน ถือว่า เป็นการหาสิ่งซัพพอร์ตที่ถูกจุด แน่นอนว่าสิ่งเหล่านี้เป็นของคู่กัน เพราะการเดิมพัน ต้องอาศัยดวงผู้เล่น เพราะไม่สามารถรู้ หรือคำนวณล่วงหน้าได้นั่นเอง

AMB said...

ทำเงินเเสนอ้าเเขนรับทรัพย์ไปกับเกมเดิมันที่หลากหลาย ครบวงจรที่สุด AMB BET เว็บตรงไม่ผ่านเอเย่นต์ ครบวงจร ปลอดภัย มาเเรงที่สุด เกมเดิมพันทุกเเขนง พร้อมให้ท่านเลือกเล่น เลือกลงทุน ไม่ว่าจะเป็น คาสิโนออนไลน์ บาคาร่า สล็อตออนไลน์ เกมยิงปลา ไฮโล เเละอื่นๆ อีกมากมาย พร้อมให้บริการทุกท่านเเล้ว เล่นง่าย ได้เงินจริง เล่นเกมเดิมพันกับเว็บไซต์ของเราได้ทุกที่ ทุกเวลา ผ่านหน้าจอโทรศัพท์มือถือ รองรับทั้งระบบ android เเละ ios เเนะนำโปร สมัครฝาก 100 รับ 200 พร้อมโปรคืนคอมเข้ายูสทุกยอดเสีย 0.8% 1User เล่นได้ทุกเกม รองรับ Wallet

Assignment Help said...

Complete Your Assignment From
HR Assignment Helper

KeiraDoltan said...

Complete Your Assignment From
Business Law Assignment Writing Service
Assignment Help

Cheap bus hire said...

This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.

Sydney Bus Hire

Assignment Help said...

This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.

Auditing Assignment Help
Assignment Help

KeiraDoltan said...

Nice post!! Students can score high marks In Their college and university assignments by hiring the best assignment help with writing homework from the experts.
Nursing Assignment Topic

usmN said...

Adding extensions to a hosted workflow can be very helpful for the programmers because it lets us do our job very easy and rapid, if you need easy and rapid cheap towing company near me service at cheap price, we are available 24/7 for you.

happy157 said...

เว็ปตรง แตกง่าย จ่ายหนัก ไม่ทำเทิร์น สล็อตเว็ปตรง พร้อมสูตร ใครยังไม่ลองอย่ารอช้า เล่นง่าย จ่ายจริง การันตีเรื่องการเงิน ใครอยากมีเงินใช้ หาเงินง่ายๆ แอตไปสมัคร รวมทุกค่ายไว้ที่นี่ที่เดียว jdb aw

joe_chef90 said...

I think adding extensions to a hosted workflow is a great idea that can help streamline processes and make them easier to manage. It's great to see essay Help UK offering this kind of service to their customers, as it makes it easier for them to get the help they need and makes their workflow more efficient.

Diane Sawyer said...

Enhance your hosted workflow by adding extensions. Improve functionality and customize it to your needs. Struggling to take my algebra class? Get help from a tutor or online resources

Sara Cron said...

This is a great article on adding extensions to a hosted workflow. As a business owner looking for the best MCA leads, having the ability to customize my workflow is crucial. The explanations and visuals provided made it easy for me to understand and implement.