public TChannel CreateLocalChannel <TChannel>() { ChannelFactory <TChannel> channelFactory = new ChannelFactory <TChannel>(localBinding, localWorkflowAddress); TChannel channel = channelFactory.CreateChannel(); IDictionary <string, string> context = ContextManager.DepersistContext(contextFileName); if (context != null && context.Count > 0) { ContextManager.ApplyContextToChannel(context, (IClientChannel)channel); recoveredContext = true; // register handlers to cleanup context file when Workflow completes or terminates. this.Description.Behaviors.Find <WorkflowRuntimeBehavior>().WorkflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { ContextManager.DeleteContext(contextFileName); }; this.Description.Behaviors.Find <WorkflowRuntimeBehavior>().WorkflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { ContextManager.DeleteContext(contextFileName); }; } return(channel); }
void PowerSwitch(object sender, RoutedEventArgs e) { try { using (new OperationContextScope((IContextChannel)Client.InnerChannel)) { if (PowerOn) { PowerOn = false; if (!contextApplied) { ContextManager.ApplyContextToChannel(Context, Client.InnerChannel); contextApplied = true; } Context = null; ContextManager.DeleteContext(contextFile); Client.PowerOff(); Client.Close(); client = null; } else { Client.PowerOn(); // At this point the channel has the context from the operation called contextApplied = true; // extract and persist context Context = ContextManager.ExtractContextFromChannel(client.InnerChannel); ContextManager.PersistContext(Context, contextFile); PowerOn = true; } } } catch (CommunicationException ce) { MessageBox.Show(ce.Message); Client.Abort(); client = null; } }
void DoOperation(object sender, RoutedEventArgs e) { int value = 0; string operation = ((Button)sender).Content.ToString(); if (!operation.StartsWith("=") && !int.TryParse(Display.Text, out value)) { MessageBox.Show("Invalid input! Try again."); Display.Text = String.Empty; } try { if (!PowerOn) { PowerSwitch(ButtonC, null); } using (new OperationContextScope((IContextChannel)Client.InnerChannel)) { if (!contextApplied) { ContextManager.ApplyContextToChannel(Context, Client.InnerChannel); contextApplied = true; } switch (operation) { case "=": { value = Client.Add(0); break; } case "+": { value = Client.Add(value); break; } case "-": { value = Client.Subtract(value); break; } case "x": { value = Client.Multiply(value); break; } case "/": { if (value == 0) { MessageBox.Show("Divide By Zero is not allowed"); value = client.Add(0); break; } else { value = Client.Divide(value); break; } } } } } catch (CommunicationException ce) { MessageBox.Show(ce.Message); Client.Abort(); client = null; } Display.Background = onBrush; Display.Text = value.ToString(); displayNew = true; }