public void CanImplementMessagingProtocol()
        {
            // Arrange
            const int Expected = 123;
            const int Increment = 3;
            var xamlInjector = new XamlInjector(Constants.ServicewithtworeceivesandincrementXamlx);

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();
            host.Extensions.Add(stubExtension);

            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", Expected);
            stubExtension.OnIdle = s =>
                {
                    // If processing the reply to the initial message
                    if (s.Messages.Count == 2 && s.QueueCount == 0)
                    {
                        // Access the content and add the increment
                        s.EnqueueReceive(
                            this.serviceContractName, "IncrementData", ((int)s.Messages[1].Content) + Increment);
                    }
                };

            try
            {
                // Act
                host.TestActivity();

                // Assert
                Assert.AreEqual(Expected + Expected + Increment, stubExtension.Messages[3].Content);
                Assert.AreEqual(0, stubExtension.QueueCount);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void ShouldHostService()
        {
            var trackingProfile = new TrackingProfile { Queries = { new ActivityStateQuery { ActivityName = "ReceiveRequest", States = { "Executing" }, }, new ActivityStateQuery { ActivityName = "SendResponse", States = { "Executing" }, }, } };

            var xamlInjector = new XamlInjector("TestSumService.xamlx");

            // The first TestActivity1 will not be replaced - will add 1 to sum

            // Replace the second TestActivity1 with TestActivity2 - will add 2 to sum
            xamlInjector.ReplaceAt(1, typeof(TestActivity1), typeof(TestActivity2));

            // Replace third TestActivity1 with TestActivity3 - will add 3 to sum
            xamlInjector.ReplaceAt(2, typeof(TestActivity1), typeof(TestActivity3));

            // Replace all (2) TestActivity4 with TestActivity5 - will add 10 to sum
            xamlInjector.ReplaceAll(typeof(TestActivity4), typeof(TestActivity5));

            // Response will be (data=1)+1+2+3+10 = 17
            var serviceAddress = ServiceTest.GetUniqueEndpointAddress();
            using (var testHost = new WorkflowServiceTestHost(xamlInjector.GetWorkflowService(), serviceAddress))
            {
                testHost.Tracking.TrackingProfile = trackingProfile;
                testHost.Open();

                var client = ChannelFactory<ITestService>.CreateChannel(ServiceTest.Pipe, serviceAddress);
                var response = client.GetData(1);
                Assert.AreEqual("17", response);

                testHost.Close();

                // Find the tracking records for the ReceiveRequest and SendResponse

                // Activity <ReceiveRequest> state is Executing
                AssertTracking.ExistsAt(testHost.Tracking.Records, 0, "ReceiveRequest", ActivityInstanceState.Executing);

                // Activity <SendResponse> state is Executing
                AssertTracking.ExistsAt(testHost.Tracking.Records, 1, "SendResponse", ActivityInstanceState.Executing);
            }
        }
        public void CanSendToAnyParallelBranch()
        {
            var xamlInjector = new XamlInjector(Constants.ServiceWithParallelThreeReceivesXamlx);
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));
            var host = new WorkflowInvokerTest(xamlInjector.GetWorkflowService().Body);
            var stubExtension = new MessagingStubExtension();
            host.Invoker.Extensions.Add(stubExtension);

            stubExtension.EnqueueReceive(this.serviceContractName, "GetData3", 3);
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData1", 1);
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData2", 2);

            try
            {
                host.TestActivity();

                Assert.AreEqual(6, stubExtension.Messages.Count);
                Assert.AreEqual("3", stubExtension.Messages[1].Content);
                Assert.AreEqual("1", stubExtension.Messages[3].Content);
                Assert.AreEqual("2", stubExtension.Messages[5].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void CanMockReceiveAndSendReply()
        {
            // Arrange
            const int Expected = 123;
            var xamlInjector = new XamlInjector(Constants.DefaultServiceXamlx);

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();
            host.Extensions.Add(stubExtension);

            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", 123);

            try
            {
                // Act
                host.TestActivity();

                // Assert
                Assert.AreEqual(Expected.ToString(), stubExtension.Messages[1].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }