Ejemplo n.º 1
0
        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;
            }
        }
        public IDictionary <string, string> MaintainContext(IClientChannel channel)
        {
            IDictionary <string, string> context = null;

            if (!RecoveredContext)
            {
                context = ContextManager.ExtractContextFromChannel((IClientChannel)channel);
                ContextManager.PersistContext(context, contextFileName);
                // register handlers to cleanup context file when Workflow completes or terminates.
                wfRuntime.WorkflowCompleted  += delegate(object sender, WorkflowCompletedEventArgs e) { ContextManager.DeleteContext(contextFileName); };
                wfRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { ContextManager.DeleteContext(contextFileName); };
            }
            return(context);
        }
        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);
        }