Hosting workflows with WorkflowServiceHost
Ok, so I may not be approaching this correctly, but essentially I'm trying to play with the new WF services in 4.0 to create a Windows service that hosts the WF service. At this point I have created a client (containing a simple Activity XAML) and a service (a WF service implemented in a XAMLX file).
I've tried simple "hello worlds" for each one. The client is hosted in WorkflowApplication
and I initially configured the service as the default WF project template. Everything seems to be in order there.
Since I want to host a service without IIS, naturally my next attempt was to host my service in WorkflowServiceHost
. To do this, I can use XamlServices.Load()
and pass the object it returns to the constructor WorkflowServiceHost
, along with the URI for the endpoint. I was concerned that there is no Run()
member method like there is in the class WorkflowApplication
. I assumed the method Open()
would open the service host object as a service and that it would start a workflow instance, but it doesn't.
First I set up the service workflow to just write to a text file when it was started but nothing happened. I tried to debug breakpoints, but since it loads the XAMLX file at runtime, VS won't let me debug WF. So I tried modifying the client project a bit to use WorkflowServiceHost
instead WorkflowApplication
. I used the same workflow I used to test the hello world style workflow and this time there was no output for the console, but it WorkflowApplication
was successful.
Here's the basics of what I did with the client to host the workflow service in a console project. If anyone would like to see the XAML for the workflow, let me know and I'll update this question. Here's the hosting code in Main()
.
const String clientAddress = "http://localhost:9998/Client";
WorkflowServiceHost wfHost = new WorkflowServiceHost( new ClientWf(), new Uri(clientAddress) );
wfHost.Open();
while( Console.ReadKey().KeyChar.ToString().ToUpper() != "X" ) { }
wfHost.Close();
a source to share
What I found out is that since you cannot directly start a WF instance that is wrapped in an object WorkflowServiceHost
, its a bit painful to start it and pretty much impossible with a simple method call like you can accomplish with a WorkflowApplication
. There is a "trick" to have WF fire, but it requires a bit of a hack that I didn't give time at this point. MSDN has a hidden link to what you can do in this scenario here under Hosting Non-Service Workflows .
This is the solution I came across: using both WorkflowServiceHost and WorkflowApplication. Why do you ask? Ok, because I was trying to do everything in one tiny little package. I also create this as a custom services model for my work, and it is better to separate the business logic (WorkflowApplication) from the whole communication implementation (WorkflowServiceHost). The firefighting service is just lovely this way because of course now my main comm. the workflow starts with a receive activity and the WorkflowSericeHosts looks for some type of messaging activity in the root of the associated workflow to start the instance.
Now I am a happy tourist. My business logic does what it should do as expected and the workflow service works fine. Better yet, I have a model where I can dynamically drop the business logic in order to dynamically configure and deploy data / crunch services. Now just to refine the callbacks to find the remote "pane", I move on to the next one.
a source to share