So, last week I wrapped up a conversation at TechReady, our internal conference, where I was talking about the integration between WF and WCF in .NET 3.5. This talk was somewhat bittersweet, it's the last conference where I'm scheduled to talk about WF 3.0/3.5, I'll start talking about WF 4.0 at PDC this fall.
There are a series of 4 demos that we'll talk about in this series:
I've gotten a lot of requests to post the code samples, so I want to do that here:
The goal of this sample is to show the way that the context channel works, and how to interact with it from imperative code.
1: public String returnValue = default(System.String);
2: public String inputMessage = default(System.String);
3:
4: private void codeActivity1_ExecuteCode(object sender, EventArgs e)
5: {
6: returnValue = string.Format("first activity {0}", inputMessage);
7: Output(inputMessage + " Activity 1");
8: }
9:
10: private void Output(string message)
11: {
12: Console.WriteLine("Workflow {0} : Message {1}", this.WorkflowInstanceId, message);
13: }
14:
15: private void codeActivity2_ExecuteCode(object sender, EventArgs e)
16: {
17: returnValue = string.Format("second activity {0}", inputMessage);
18: Output(inputMessage + " Activity 2");
19: }
class IWorkflowClient : ClientBase<Intro1.IWorkflow1>, Intro1.IWorkflow1 { public IWorkflowClient() : base() { } public IWorkflowClient(Binding binding, EndpointAddress address) : base(binding, address) { } public string Hello(string message) { return base.Channel.Hello(message); } }
private static void CheckAndPrintContext(IContextManager icm) { if (null != icm) Console.WriteLine("Context contains {0} elements", icm.GetContext().Count); if (null != icm) { if (icm.GetContext().Count > 0) { foreach (string xmlName in icm.GetContext().Keys) { Console.WriteLine("key : {0}", xmlName); Console.WriteLine("value : {0}", icm.GetContext()[xmlName]); } } } }
1: private static void DemoOne()
2: {
3: Console.WriteLine("Press Enter to Send a Message and reuse proxy");
4: // Console.ReadLine();
5: Debugger.Break();
6: IWorkflowClient iwc = new IWorkflowClient(new NetTcpContextBinding(),
7: new EndpointAddress("net.tcp://localhost:10001/Intro1"));
8: IContextManager icm = iwc.InnerChannel.GetProperty<IContextManager>();
9: if (null != icm) Console.WriteLine("Context contains {0} elements", icm.GetContext().Count);
10: string s = iwc.Hello("message1");
11: Console.WriteLine("the service returned the message '{0}'", s);
12: CheckAndPrintContext(icm);
13: s = iwc.Hello("message2");
14: icm = iwc.InnerChannel.GetProperty<IContextManager>();
15: CheckAndPrintContext(icm);
16: Console.WriteLine("the service returned the message '{0}'", s);
17: Console.WriteLine("Press Enter to Continue");
18: }
1: // show this not working using a second client
2: private static void DemoTwo()
3: {
4: Console.WriteLine("Press Enter to Send a Message (it will break this time)");
5: //Console.ReadLine();
6: Debugger.Break();
7: IWorkflowClient iwc = new IWorkflowClient(new NetTcpContextBinding(),
8: new EndpointAddress("net.tcp://localhost:10001/Intro1"));
9: IContextManager icm = iwc.InnerChannel.GetProperty<IContextManager>();
10: if (null != icm) Console.WriteLine("Context contains {0} elements", icm.GetContext().Count);
11: string s = iwc.Hello("message1");
12: Console.WriteLine("the service returned the message '{0}'", s);
13: CheckAndPrintContext(icm);
14: iwc = new IWorkflowClient(new NetTcpContextBinding(),
15: new EndpointAddress("net.tcp://localhost:10001/Intro1"));
16: s = iwc.Hello("message2");
17: Console.WriteLine("the service returned the message '{0}'", s);
18: icm = iwc.InnerChannel.GetProperty<IContextManager>();
19: CheckAndPrintContext(icm);
20: Console.WriteLine("Press Enter to Continue");
21: }
1: // show this working with a second client by caching the context
2: private static void DemoThree()
4: Console.WriteLine("Press Enter to Send a Message (we'll cache the context and apply it to the new proxy)");
5: // Console.ReadLine();
14: IDictionary<string, string> context = icm.GetContext();
15: icm = null;
16: iwc = new IWorkflowClient(new NetTcpContextBinding(),
17: new EndpointAddress("net.tcp://localhost:10001/Intro1"));
19: icm.SetContext(context);
20: s = iwc.Hello("message2");
21: Console.WriteLine("the service returned the message '{0}'", s);
22: icm = iwc.InnerChannel.GetProperty<IContextManager>();
23: CheckAndPrintContext(icm);
24: Console.WriteLine("Press Enter to Exit");
25:
26: }
So, what have we shown: