Management Tab Missing in Workspace

I recently ran into a problem after an upgrade to 1420 whereby the Management tab was missing from the workspace. I did all the regular checks (license, permissions etc) and no luck. If you ever run into this situation, back up your K2Workspace database and then truncate the ActionPermissions table. This resolved it for me.

Thanks to Tyler Clark from K2 for helping me out with this one.

K2 and Entity Framework

I’m taking a quick break from WF4 to blog about some fun we’re having with Entity Framework and K2. I’ll get back to WF4 straight away…

Our situation is as follows: Our workflow directly references a class library (our application’s business layer) and the class library uses Entity Framework to access the application database. The first event in our workflow is a code event which loads up some config entries from this class library (which in turns gets them from our database) and then assigns a task to the user. We’re starting our workflows through the API and we’re doing it synchronously, which means the method doesn’t return until the K2 process reaches a stable state like a client event.

Here’s the issue – the first call to start a process instance takes 35 seconds. After that they are all quick, but after a bit of inactivity it takes 35 seconds again.

To understand what’s happening you need to understand the inner workings of Entity Framework, K2 and AppDomains. The actual cause of the performance issue is Entity Framework. I won’t go into all the details (because we don’t need them) but when you first query a database using Entity Framework a model needs to be created. This takes a lot of time. There is also some connection pooling, which also takes time. The important point here is that the model and the connection pool are cached and kept alive within the context of an AppDomain.

To make it more complicated, K2 has designed their application such that each process definition has it’s own AppDomain (which is of course the correct design decision). This means that if you have 2 different process definitions referencing the same class library you will have 2 AppDomains, 2 cached models, 2 connection pools. K2 also unloads and reloads these AppDomains after 15 minutes of inactivity, so if nothing happens with ProcessA for 16 minutes and you start a new one, you’re going to see that long delay.

So what can you do? Unfortunately, not much. I tried hosting our class library in a WCF service in the hope that this would keep the model cached outside of our K2 process’ AppDomain but it doesn’t work that way. You can’t use a ‘Keep Alive’ script because the script would need to be run in the same AppDomain as the model, and unless you’re going to build that into each of your K2 processes that’s not going to work. You can, however, try one of the following options:

  1. Start your processes asynchronously. We may have to go this route.
  2. Set the AppDomainUnload timeout value to something greater than 15 minutes. To do this you set the “AppDomainUnload” setting in the \Host Server\bin\K2Server.setup file on the K2 server to something like 8 hours: <AppDomainUnload Minutes=”480″ />

Setting AppDomainUnload to a value of “0″ turns off AppDomain unloading completely, but don’t ever do this or you will run into memory usage problems.

I hope this helps someone.

Windows Workflow Foundation Episode 3

Welcome to part 3 of Windows Workflow Foundation 4 for K2 developers. In part 1 I outlined the differences between how you should approach K2 and WF4, and in part 2 I spoke about the different designer options you have and mentioned a few best practices. There is 1 more topic I need to discuss before we can start building our workflow engine, and that is the topic of workflow persistance.

As I mentioned, WF4 is not a service – it’s baked into your application, so when your application thread stops running, so does your workflow. If we’re going to be turning this into a service then we’re going to need a way of maintaining workflow state. We’ll be doing this by serialising our workflows and persisting them in a database.

Step 1 – Creating the SQL Store

Microsoft have made this simple for us by providing us with the store to use. Assuming you have .Net 4 installed on your machine you should be able to navigate to [C:\Windows\Microsoft.NET\Framework\v4.0.30319\SQL\en] and you’ll see a bunch of SQL scripts. 2 of these are important to us:

  • SqlWorkflowInstanceStoreSchema.sql – This script will provide you with the schema you need to persist your workflow
  • SqlWorkflowInstanceStoreLogic.sql – This adds the logic to the schema

Start by creating a new database called WorkflowInstanceStore, then execute the above scripts against the database. That’s it – you’re done.

Step 2 – Set your workflows up to automatically persist

What we want to accomplish here is to get your workflows to automatically persist whenever the workflow is idle, such as when it’s waiting for a user to do something. We will also need a way of referencing this workflow again at a later stage (so you’ll need to save the workflow ID) and you’ll also need to be able to tell the workflow which activity you’re wanting to resume, and for this you need a Bookmark (a bookmark is just a string). I’ll show you how to get your workflow to automatically persist itself to the database by creating an activity which listens for the idle event. Here’s what you will do (we will implement this later – this is just pseudocode to get the idea across)

  1. Create a code activity which inherits from NativeActivity and call it WaitForSomething
  2. Add this method in:
  3. protected override bool CanInduceIdle
    {
       get { return true; }
    }
  4. Create a new workflow and somewhere in the workflow you should add your WaitForSomething activity. Now when you start your workflow, you’ll do it this way:
  5. WorkflowApplication wfApp = new WorkflowApplication(MyWorkflowDefinition);
    SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(connectionString);
    wfApp.InstanceStore = store;
    wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
    {
       // Instruct the runtime to persist and unload the workflow.
       return PersistableIdleAction.Unload;
    };

I’ll stop here and recap what we’ve done. Firstly, we created an activity which raises a PersistableIdle event. Then we added this activity to a workflow and created a delegate to handle the PersistableIdle event. The event returns Unload’ which tells WF4 to unload the process from memory and persist it to the sql store referenced by the connection string we provided.

Right – I think I’ll stop there because if we go any further without doing some examples we’re going to get lost. In the next post we will start building our project and I’ll include the project for you to download so you can follow along.

As always, please send any and all questions my way.

Windows Workflow Foundation Episode 2

[Yes - I know I only published episode 1 a few minutes ago, but I wrote these 2 posts on a 14 hour plane trip]

Welcome back to WF4 for K2 developers. In my earlier post I spoke about the differences between K2 and WF4. In this post I’ll speak about the workflow designer options you have and some best practices.

There are a number of ways you can build your workflows, each with pros and cons

  1. Firstly there’s the graphical designer. All you K2 gurus will leap straight into this one, but keep in mind that there are no line rules – you have to use decision points to build in your business logic. Also, there are several options for the graphical designer – you could use a sequence, a flow chart, a state machine, or any combination of these. Again, each of these have their pros and cons, but if you’re looking for something similar to the K2 canvas then try a flow chart. True to style, the graphical designer is purely a front end to generate XAML, which leads me to the second option…
  2. XAML. If you’re happy coding in XAML then you can do it all in XAML. Personally I don’t see the point…
  3. Code. Anything you can do from the designer you can do in code, and I have to say that it’s very well thought out – designing a workflow in code is simple, quick and easy to follow (to a point).

So which should you use? The answer is, of course, that it depends on the workflow you’re designing. Here are some guidelines which should help you choose…

  • For small workflows, use code. There is a VERY tiny performance improvement, it’s easy to edit the functionality, but the biggest advantage is the ability to use the built-in refactoring functionality. If you’re using XAML or the designer, you’re refactoring manually. Of course if your workflow is coded then you can step through it in the debugger as well.
  • For all other workflows, use the visual designer. As nice as the code is, it quickly becomes difficult to follow the business logic and you get lost in a sea of confusion.
  • For those of you in the real world, you will use both. When I am building a WF4 app I create a project to hold commonly-used custom activities which I have written. Some of these are in code, some are in XAML, but when I build the actual workflow it’s always in the designer. The only thing I would say is that there’s no point in writing a workflow in XAML from scratch. It’s like coding C# in notepad. Yes, you can do it, but why would you?
  • We have an approach in Dynamyx which is “everything is a framework”. Try make your approach to your workflows as generic as possible. Look for reusable sections and turn those into separate custom activities. Build up a library of generic activities for use in other projects. When we start building our workflow engine you’ll see what I mean.

Finally, if you’re using code activities you will be writing classes which will inherit from one of several base classes. By default you should either use CodeActivity or it’s asynchronous counterpart since these are lightweight. If you need more advanced features (like automatic persistence when your activity is idle) then you need to inherit from NativeActivity. There are others, but these cover 99% of the cases you will see.

In the next post I’ll speak about workflow persistence and events since we’ll need a good understanding of how this works before we you can dive into developing the workflow engine.

Windows Workflow Foundation Episode 1

A wise man once said that if all you have is a hammer then everything you see is a nail. A wiser man (Scott Adams) pointed out that if all you have is a hammer then you’re not going to be thinking about nails because you’ll be using your hammer to hide your naughty bits on account of you being naked (seeing as all you have is a hammer). I reckon they were both right. The point is that even as a K2 consultant it’s worth knowing what the other workflow vendors are doing in the workflow world.

I have been working on a workflow project where K2 wasn’t an option. I dutifully looked at other workflow options (there are a few open source options out there) but none of them ticked enough boxes. Eventually I decided that my best option was to write my own workflow engine using windows workflow foundation.

Since you’re reading this blog I’ll assume that you are either a K2 developer, or you have no life at all and need to get out more. I’ll give you the benefit of the doubt. I’m going to be writing a series of blogs here describing windows workflow foundation from the point of view of a K2 developer. Windows workflow foundation 4 is not difficult, but if you approach it like a less-featured K2 (which is what I did) you’re going to get nowhere. Hopefully this will make it easier.

In this blog I’ll be outlining the differences between WF4 and K2 and how you should approach them differently. In later posts I’ll walk you through the process of wrapping WF4 up into a workable workflow engine.

So if you’re a K2 developer looking at WF4 for the first time, these are the things you need to keep in mind or you will get totally lost:

  1. In WF4 there is no difference between an activity and a workflow. When you start a workflow you are actually starting a new activity. You can create complex activities (similarly to how you can create a usercontrol which contains other controls) but they all become part of the same workflow. So there’s no concept of an IPC call here. In fact, you can create 2 complex workflows and add those workflows as child activities in a parent workflow, and all the workflows will have the same instance GUID assigned to them. So when I speak about a workflow I could be speaking about an activity or a group of activities as well.
  2. In WF4 there’s no concept of tasks being assigned to people. There’s no AD integration. Basically there are server tasks and that’s it.
  3. In WF4 the workflow is baked into the application. This means that when your application is running, your workflow is alive and accessible. When your app is closed, the workflow is closed as well. This is a biggie. There’s no concept of making a call to a workflow engine. You can persist your workflow to a database in order to save state but if you close your application and hope that an escalation rule will fire in the background you’reout of luck. Of course you can create a WCF service to host your workflow app, or you could use AppFabric. We will obviously do something along these lines.

Those are the main differences – if you can get your head around these points you’re well on your way to understanding WF4. The next post will speak about the workflow designer and mention some best practices. If you have any questions during the series, please feel free to ask…

Moving K2 databases?

My first Making Flow Work post! Feels like I’m cheating on the Naked Programmer.

Sometimes you need to move K2′s databases from one SQL server to another. Various reasons might force you to do it, specifically when you realised you should probably not have put the K2 databases on the same SQL Server as SharePoint 2010.

It’s a straight forward process and includes two parts:
1. Backup and restore all the K2 databases to a new SQL Server
2. Recreate the SCSSOKey symmetric keys and SCHostServerCert certificates

The following two articles explain the whole processes step by step:

http://help.k2.com/en/KB000591.aspx

http://help.k2.com/helppages/k2blackpearl1370/Backing_up_Keys_and_Certificates.html

I did however discover that, even after running the K2 Setup Manager to reconfigure K2 to point to the new SQL server, the out of the box K2 reports stopped working and that users could no longer create custom worklist filters (and I assume various other user specific configuration features must have been broken). Users trying to run OOB report got this error: An error occurred loading the report. Service: K2Generic Settings Service Guid: ffffffff-ffff-ffff-ffff-fffffffffffff Severity: Informational Error Message: Cannot open database “K2HostServer”. Users trying to create worklist filters got: “Could not save user data. Check error logs for more information”.

It seems that there is a service broker instance called the Generic Settings Service that contains a link the K2HostServer database and the link is not updated by the K2 Setup Manager. After manually updating the link, everything worked fine.

Getting child process IDs after IPC Event

I like IPC events and use them all the time – if you design your processes well with IPC events then you can keep them small and manageable, and each time you get to an IPC event your process is bumped up to the latest version so it’s easier to propagate bug fixes. However I recently needed to keep track of the child process IDs and I realised that you can’t do this using the IPC event wizard. I tried to map the child process ID to an activity level data field in the parent process but unfortunately I couldn’t add the process ID field from the child process as a source.

So how can you do it?

My implementation was to pass the parent process ID into the child process, and then write these IDs to a database in the first activity of the child process:

Child Process

Another way to do it is to include the parent process instance ID in the folio of the child process, then when you retrieve the worklist you can filter the worklist by folio. This is a simple way of doing it but isn’t the most efficient.

Hope that helps.

K2.ActivityInstanceDestination.User is NULL?????

This one caught me by surprise! I needed to build up a CSV list of email addresses for all previous task owners for an activity, so the simplest way would be to just append the email address each time we get to the task, right? Apparently not.

Here’s my code:

K2.ProcessInstance.DataFields["AllApproversEmailAddresses"].Value = K2.ActivityInstanceDestination.User.Email;

Nothing too complicated. However, running it gave me the dreaded (and oh so informative) “Object reference not set to an instance of an object”. So what’s happening?

Well, it’s actually not that complicated. Basically when you create any activity the default destination rule is “Plan Just Once” (in fact you don’t even have the option of choosing anything else unless you run the Activity wizard in advanced mode.) Anyway, when this option is chosen the destination instance isn’t actually created, which means that K2.ActivityInstanceDestination will always return null.

To get around this issue you will need to do the following:

  • Right-click the activity to open the activity properties.
  • Click on the Destination Rule Options tab, and then click the ‘Back’ button on the wizard.
  • Check the box which says “Run this wizard in advanced mode”, then click “Next”.
  • You’ll see that “Plan per slot (no destinations)” is selected. Choose “All at once” and then click through the rest of the wizard.
  • Deploy and test.

That’s it. Hope that helps someone!

28025 Failed to Start IPC

I came across this error again and it reminded me that I’ve come across it a number of times before, so I’ll list the possible issues here:

  1. You don’t have rights to start the child process. When you set up the IPC call the default authentication is set to ‘Integrated’ and I often forget to change this to ‘Impersonate’. When I get this 28025 error the first thing I check is whether or not I’ve set the authentication to impersonate and that the user has start rights on the child process.
  2. The data field you’re setting in the child process doesn’t exist. I came across this when I created my data field in the child process while I was going through the wizard to set up the IPC call. The problem was when I finished the wizard and everything compiled and exported, the data field I created hadn’t actually been created. One day if I have time I’ll raise a bug for this…
  3. You’re passing data with incompatible types. You can’t pass a string from the parent process to an int data field in the child process, for example.

Are you getting the most value from K2 blackpearl?

As a BPM architect and business analyst I’m in the fairly unique position of having seen how companies all over the world are using K2 blackpearl. It would be nice to be able to say that they are all going about it the right way, but I find that most companies aren’t. That’s not in any way judging anyone, but in reality the best way of using any techology comes from experience, and in most cases companies don’t have the time (or budget) to spend getting experience and building up the expertise to tackle a workflow project using industry best practices.

This isn’t going to be a K2 blackpearl best practices guide, but here are a few checks and balances to make sure you’re on the right track:

1. Get results fast.

As a rule of thumb I feel that I and my team should be able to deliver a useable, valuable workflow project within 3 months. If it takes longer than that then I have a good look at the project – sometimes it’s a large project and there’s a good reason for a longer timescale – but our rule of thumb is 3 months.

2. Choose the right technology.

Spend the time at the start of the project to choose the right tecchnology to fit around K2. In many cases this is simple – you can safely go straight to Microsoft SQL Server and know you’ve chosen well. The business part is usually simple as well – you’re going to be using .Net. The choice of UI technology, however, is critical. Are you going to use ASP.Net? Forms? InfoPath? SharePoint lists? All have their pros and cons, but if you make the wrong choice here you’re going to pay for it over and over again. This leads me to…

3. Fit the requirements to the tech, not the other way round.

Once you’ve chosen your tech, accept the limitations and live with them. There’s no silver bullet technology to solve all your problems. The first thing you want to do at the start of the project is print out a big sign which says “it is what it is” and hang it on the wall in the meeting room.

The most common time I see people struggling with this is with InfoPath. InfoPath is a great technology for getting results fast. I can get a great looking UI done with back end integration in almost no time at all, especially with SmartObject technology. But it’s not ASP.Net. I’ve seen people take InfoPath so far beyond what it’s meant to do that the result is completely un-useable. But I’ve also seen a company which asked “what is InfoPath good at”, they focused on those areas and the result is probably the most successful K2 project I’ve ever seen.

4. Invest time in the API.

Blackpearl has a great API. It deserves to be treated like any API, i.e. don’t just start coding and hope for the best – dig deep first and then build up a re-useable framework using the API. We do this for every project we do, and now we have a well-written, well-documented re-useable, TESTED, generic K2 framework to pop into any project. And no – our framework isnt’ for sale :)

5. Buy K2 skills into your project.

I once worked for a .Net startup in London. The company was started by about 15 consultants from a major consultancy company who thought that they could send their consultants on a 2 week .Net course and then write enterprise software. After a few months they hired me. The first thing I told them to do was to delete everything and start again from scratch. What a way to make friends!

My point here is that there is a very experienced community out there, and it’s worth investing in getting it done right. This is possibly the most important point I can make. Buy in the right skills. You have a number of options:

  1. Hire an architect with K2 experience.
  2. Contact K2 directly and ask them to recommend some help.
  3. Contact a K2 partner.

You don’t need to outsource your whole project. At the very least, if your budget is tight you can buy 2 weeks of time from a recommended K2 partner and get your architecture and design correct from day 1. We do this all the time for people. If you start off in the wrong direction you will pay for it in the long run. Again, and again, and again, and again…

Hope that helps!