public static void BehaviorExtension_ContractBehaviorAttribute()
    {
        string  testMessageBody = "BehaviorExtension_ContractBehaviorAttribute";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <IBehaviorExtensionServerInterface2>(binding, address);

        // We rely on the implicit open of the channel to be synchronous.
        // This is true for both the full framework and this NET Core version.
        IBehaviorExtensionServerInterface2 channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Message outputMessage = channel.Process(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        var result = outputMessage.GetBody <string>();

        ((IClientChannel)channel).Close();
        factory.Close();


        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        Assert.True(TestContractBehaviorAttribute.contractBehaviorSet, "TestContractBehavior attribute on IBehaviorExtensionServerInterface2 constract should have triggered ApplyClientBehavior() call");
    }
Пример #2
0
    public static void MessageContract_RoundTrips_Properties_And_Headers()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel       mockRequestChannel             = null;
        RequestBankingData_4_4_0 request = null;
        RequestBankingData_4_4_0 reply   = null;

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // When the channel is created, override the Request method to capture properties and headers
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                mockRequestChannel.RequestOverride = (msg, timeout) =>
                {
                    // Echo back a fresh copy of the request
                    MessageBuffer buffer   = msg.CreateBufferedCopy(Int32.MaxValue);
                    Message       replyMsg = buffer.CreateMessage();
                    return(replyMsg);
                };

                return(mockRequestChannel);
            };

            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <IMessageContractRoundTrip_4_4_0>(binding, address);
        IMessageContractRoundTrip_4_4_0 channel = factory.CreateChannel();

        // Prepare a strongly-typed request
        request                     = new RequestBankingData_4_4_0();
        request.accountName         = "My account";
        request.transactionDate     = DateTime.Now;
        request.requestTestProperty = "test property";
        request.testValue           = "test value";
        request.testValues          = new string[] { "test", "values" };
        request.testValuesArray     = new string[] { "test", "values", "array" };

        // *** EXECUTE *** \\
        reply = channel.MessageContractRoundTrip(request);

        // *** VALIDATE *** \\
        Assert.True(String.Equals(request.requestTestProperty, reply.requestTestProperty),
                    String.Format("TestProperty expected = '{0}' actual = '{1}'",
                                  request.requestTestProperty, reply.requestTestProperty));

        ValidateArray("TestValues", request.testValues, reply.testValues);
        ValidateArray("TestValuesArray", request.testValuesArray, reply.testValuesArray);
    }
    public override BindingElement Clone()
    {
        MockTransportBindingElement element = new MockTransportBindingElement();

        // Propagate the overrides
        element.SchemePropertyOverride      = this.SchemePropertyOverride;
        element.BuildChannelFactoryOverride = this.BuildChannelFactoryOverride;
        return(element);
    }
Пример #4
0
    public static void CustomChannel_InteractiveInitializer_Can_Be_Blocked()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to observe whether
        // its methods are invoked.
        bool beginInitializeCalled = false;
        bool endInitializeCalled   = false;
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();

        mockInitializer.BeginDisplayInitializationUIOverride = (chnl, callback, state) =>
        {
            beginInitializeCalled = true;
            return(mockInitializer.DefaultBeginDisplayInitializationUI(chnl, callback, state));
        };

        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            endInitializeCalled = true;
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // block attempts to use interactive UI
        ((IClientChannel)channel).AllowInitializationUI = false;

        // *** EXECUTE *** \\
        Assert.Throws <InvalidOperationException>(() => ((IClientChannel)channel).DisplayInitializationUI());

        // *** VALIDATE *** \\
        Assert.False(beginInitializeCalled, "BeginDisplayInitializationUI should not have been called.");
        Assert.False(endInitializeCalled, "EndDisplayInitializationUI should not have been called.");
    }
Пример #5
0
    public static void CustomChannel_Async_Request_Exception_Propagates()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel       = null;
        string             testMessageBody          = "CustomChannelTest_Async";
        Message            inputMessage             = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);
        string             expectedExceptionMessage = "Async exception message";

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory so we can intercept channel creation
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());

            // Override the OnCreateChannel call so we can fault inject an exception
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the mock channel and fault inject an exception in the EndRequest
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                mockRequestChannel.EndRequestOverride = (IAsyncResult ar) =>
                {
                    throw new InvalidOperationException(expectedExceptionMessage);
                };
                return(mockRequestChannel);
            };
            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);
        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        // The ProcessAsync should not throw -- the fault comes in the EndRequest
        Task <Message> processTask = channel.ProcessAsync(inputMessage);

        InvalidOperationException actualException = Assert.Throws <InvalidOperationException>(() =>
        {
            // awaiting the task will invoke the EndRequest
            processTask.GetAwaiter().GetResult();
        });

        // *** VALIDATE *** \\
        Assert.True(String.Equals(expectedExceptionMessage, actualException.Message),
                    String.Format("Expected exception message to be '{0}' but actual was '{1}'",
                                  expectedExceptionMessage, actualException.Message));
    }
Пример #6
0
    public static void CustomChannel_InteractiveInitializer_Error_Blocks_Open()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to throw during initialization
        InvalidOperationException         thrownException = new InvalidOperationException("ui initialization failed");
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();

        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            // Throw from the EndDisplayInitializationUI call to simulate a UI
            // action that throws an error to prevent the open.
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
            throw thrownException;
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        // The implicit open does the call to display interactive UI.
        InvalidOperationException caughtException =
            Assert.Throws <InvalidOperationException>(() => channel.Process(inputMessage));

        // *** VALIDATE *** \\
        Assert.True(string.Equals(thrownException.Message, caughtException.Message),
                    String.Format("Expected exception message to be '{0}' but actual was '{1}'",
                                  thrownException.Message, caughtException.Message));
    }
Пример #7
0
    public static void CustomChannel_ChannelInitializer_Failed_Initialize_Throws()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to fault inject an exception
        InvalidOperationException thrownException = new InvalidOperationException("test threw this");
        MockChannelInitializer    mockInitializer = new MockChannelInitializer();

        mockInitializer.InitializeOverride = (chnl) =>
        {
            mockInitializer.DefaultInitialize(chnl);
            throw thrownException;
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        InvalidOperationException caughtException =
            Assert.Throws <InvalidOperationException>(() => factory.CreateChannel());

        // *** VALIDATE *** \\
        Assert.True(String.Equals(caughtException.Message, thrownException.Message),
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                  thrownException.Message, caughtException.Message));
    }
Пример #8
0
    public static void CustomChannel_ChannelInitializer_CreateChannel_Calls_Initialize()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to observe whether
        // its method is invoked.
        bool initializeCalled = false;
        MockChannelInitializer mockInitializer = new MockChannelInitializer();

        mockInitializer.InitializeOverride = (chnl) =>
        {
            initializeCalled = true;
            mockInitializer.DefaultInitialize(chnl);
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        factory.CreateChannel();

        // *** VALIDATE *** \\
        Assert.True(initializeCalled, "Initialize was not called.");
    }
Пример #9
0
    public static void CustomChannel_Async_Open_Close_Methods_Called()
    {
        MockChannelFactory<IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel = null;
        List<string> channelOpenMethodsCalled = new List<string>();
        List<string> channelCloseMethodsCalled = new List<string>();
        List<string> factoryOpenMethodsCalled = new List<string>();
        List<string> factoryCloseMethodsCalled = new List<string>();
        string testMessageBody = "CustomChannelTest_Async";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func<Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory and intercept all open and close method calls
            mockChannelFactory = new MockChannelFactory<IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            MockCommunicationObject.InterceptAllOpenMethods(mockChannelFactory, factoryOpenMethodsCalled);
            MockCommunicationObject.InterceptAllCloseMethods(mockChannelFactory, factoryCloseMethodsCalled);

                // Override the OnCreateChannel call so we get the mock channel created by the factory
                mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the default mock channel and intercept all its open and close method calls
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                MockCommunicationObject.InterceptAllOpenMethods(mockRequestChannel, channelOpenMethodsCalled);
                MockCommunicationObject.InterceptAllCloseMethods(mockRequestChannel, channelCloseMethodsCalled);

                return mockRequestChannel;
            };
            return mockChannelFactory;
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Explicitly open factory asynchronously because the implicit open is synchronous
        IAsyncResult openResult = factory.BeginOpen(null, null);
        Task openTask = Task.Factory.FromAsync(openResult, factory.EndOpen);
        openTask.GetAwaiter().GetResult();

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Task<Message> processTask = channel.ProcessAsync(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        Message outputMessage = processTask.GetAwaiter().GetResult();
        var result = outputMessage.GetBody<string>();

        // Explicitly close the channel factory asynchronously.
        // One of the important aspects of this test is that an asynchronous
        // close of the factory also asynchronously closes the channel.
        IAsyncResult asyncResult = factory.BeginClose(null, null);
        Task task = Task.Factory.FromAsync(asyncResult, factory.EndClose);
        task.GetAwaiter().GetResult();

        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        string expectedOpens = "OnOpening,OnBeginOpen,OnOpened";
        string expectedCloses = "OnClosing,OnBeginClose,OnClosed";

        string actualOpens = String.Join(",", channelOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
               String.Format("Expected channel open methods to be '{0}' but actual was '{1}'.",
                             expectedOpens, actualOpens));

        string actualCloses = String.Join(",", channelCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected channel close methods to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));

        actualOpens = String.Join(",", factoryOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
               String.Format("Expected factory open methods to be '{0}' but actual was '{1}'.",
                             expectedOpens, actualOpens));

        actualCloses = String.Join(",", factoryCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected factory close methods to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));

        Assert.True(factory.State == CommunicationState.Closed,
            String.Format("Expected factory's final state to be Closed but was '{0}'", factory.State));

        Assert.True(((ICommunicationObject)channel).State == CommunicationState.Closed,
            String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));

    }
Пример #10
0
    public static void MessageContract_Builds_Correct_Request_Message_Properties_And_Headers()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel                  mockRequestChannel  = null;
        RequestBankingData_4_4_0            request             = null;
        Dictionary <string, string>         properties          = new Dictionary <string, string>();
        Dictionary <string, List <string> > headers             = new Dictionary <string, List <string> >();

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // When the channel is created, override the Request method to capture properties and headers
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                mockRequestChannel.RequestOverride = (msg, timeout) =>
                {
                    // Capture properties and headers before the message is read and closed
                    foreach (var property in msg.Properties)
                    {
                        properties[property.Key] = property.Value.ToString();
                    }

                    foreach (MessageHeader header in msg.Headers)
                    {
                        List <string> values = null;
                        if (!headers.TryGetValue(header.Name, out values))
                        {
                            values = new List <string>();
                            headers[header.Name] = values;
                        }

                        string headerValue = header.ToString().Replace(Environment.NewLine, String.Empty).Replace("  ", String.Empty);
                        values.Add(headerValue);
                    }

                    // Echo back a fresh copy of the request
                    MessageBuffer buffer = msg.CreateBufferedCopy(Int32.MaxValue);
                    return(buffer.CreateMessage());
                };

                return(mockRequestChannel);
            };

            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://*****:*****@"<?xml version=""1.0"" encoding=""utf-16""?>" +
                       @"<h:Request_SingleRequestValue xmlns:h=""http://www.contoso.com"">test value</h:Request_SingleRequestValue>");

        ValidateHeader(headers, TestTypeConstants_4_4_0.MessageContract_Request_MultipleValueName,
                       @"<?xml version=""1.0"" encoding=""utf-16""?>" +
                       @"<h:Request_MultipleRequestValue xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:h=""http://www.contoso.com"">" +
                       @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">test</string>" +
                       @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">values</string>" +
                       @"</h:Request_MultipleRequestValue>");

        ValidateHeader(headers, TestTypeConstants_4_4_0.MessageContract_Request_MultipleArrayValueName,
                       @"<?xml version=""1.0"" encoding=""utf-16""?>" +
                       @"<h:Request_MultipleArrayRequestValue xmlns:h=""http://www.contoso.com"">test</h:Request_MultipleArrayRequestValue>",

                       @"<?xml version=""1.0"" encoding=""utf-16""?>" +
                       @"<h:Request_MultipleArrayRequestValue xmlns:h=""http://www.contoso.com"">values</h:Request_MultipleArrayRequestValue>",

                       @"<?xml version=""1.0"" encoding=""utf-16""?>" +
                       @"<h:Request_MultipleArrayRequestValue xmlns:h=""http://www.contoso.com"">array</h:Request_MultipleArrayRequestValue>");
    }
Пример #11
0
    public static void CustomChannel_Factory_Abort_Aborts_Channel()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel        = null;
        List <string>      channelOpenMethodsCalled  = new List <string>();
        List <string>      channelCloseMethodsCalled = new List <string>();
        List <string>      factoryOpenMethodsCalled  = new List <string>();
        List <string>      factoryCloseMethodsCalled = new List <string>();
        string             testMessageBody           = "CustomChannelTest_Sync";
        Message            inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory and intercept all open and close method calls
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            MockCommunicationObject.InterceptAllOpenMethods(mockChannelFactory, factoryOpenMethodsCalled);
            MockCommunicationObject.InterceptAllCloseMethods(mockChannelFactory, factoryCloseMethodsCalled);

            // Override the OnCreateChannel call so we get the mock channel created by the factory
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the mock channel and intercept all its open and close method calls
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                MockCommunicationObject.InterceptAllOpenMethods(mockRequestChannel, channelOpenMethodsCalled);
                MockCommunicationObject.InterceptAllCloseMethods(mockRequestChannel, channelCloseMethodsCalled);
                return(mockRequestChannel);
            };
            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // We rely on the implicit open of the channel to be synchronous.
        // This is true for both the full framework and this NET Core version.
        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Message outputMessage = channel.Process(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        var result = outputMessage.GetBody <string>();

        // Abort the factory and expect both factory and channel to be aborted.
        factory.Abort();

        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        string expectedOpens  = "OnOpening,OnOpen,OnOpened";
        string expectedCloses = "OnClosing,OnAbort,OnClosed";

        string actualOpens = String.Join(",", channelOpenMethodsCalled);

        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected channel open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        string actualCloses = String.Join(",", channelCloseMethodsCalled);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected channel close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        actualOpens = String.Join(",", factoryOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected factory open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        actualCloses = String.Join(",", factoryCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected factory close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        Assert.True(factory.State == CommunicationState.Closed,
                    String.Format("Expected factory's final state to be Closed but was '{0}'", factory.State));

        Assert.True(((ICommunicationObject)channel).State == CommunicationState.Closed,
                    String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));
    }
Пример #12
0
    public static void CustomChannel_Factory_Abort_Aborts_Channel()
    {
        MockChannelFactory<IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel = null;
        List<string> channelOpenMethodsCalled = new List<string>();
        List<string> channelCloseMethodsCalled = new List<string>();
        List<string> factoryOpenMethodsCalled = new List<string>();
        List<string> factoryCloseMethodsCalled = new List<string>();
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func<Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory and intercept all open and close method calls
            mockChannelFactory = new MockChannelFactory<IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            MockCommunicationObject.InterceptAllOpenMethods(mockChannelFactory, factoryOpenMethodsCalled);
            MockCommunicationObject.InterceptAllCloseMethods(mockChannelFactory, factoryCloseMethodsCalled);

            // Override the OnCreateChannel call so we get the mock channel created by the factory
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the mock channel and intercept all its open and close method calls
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                MockCommunicationObject.InterceptAllOpenMethods(mockRequestChannel, channelOpenMethodsCalled);
                MockCommunicationObject.InterceptAllCloseMethods(mockRequestChannel, channelCloseMethodsCalled);
                return mockRequestChannel;
            };
            return mockChannelFactory;
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // We rely on the implicit open of the channel to be synchronous.
        // This is true for both the full framework and this NET Core version.
        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Message outputMessage = channel.Process(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        var result = outputMessage.GetBody<string>();

        // Abort the factory and expect both factory and channel to be aborted.
        factory.Abort();

        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        string expectedOpens = "OnOpening,OnOpen,OnOpened";
        string expectedCloses = "OnClosing,OnAbort,OnClosed";

        string actualOpens = String.Join(",", channelOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
               String.Format("Expected channel open methods to be '{0}' but actual was '{1}'.",
                             expectedOpens, actualOpens));

        string actualCloses = String.Join(",", channelCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected channel close methods to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));

        actualOpens = String.Join(",", factoryOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
               String.Format("Expected factory open methods to be '{0}' but actual was '{1}'.",
                             expectedOpens, actualOpens));

        actualCloses = String.Join(",", factoryCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected factory close methods to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));

        Assert.True(factory.State == CommunicationState.Closed,
                    String.Format("Expected factory's final state to be Closed but was '{0}'", factory.State));

        Assert.True(((ICommunicationObject)channel).State == CommunicationState.Closed,
            String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));
    }
Пример #13
0
    public static void CustomChannel_Async_Open_Close_Methods_Called()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel        = null;
        List <string>      channelOpenMethodsCalled  = new List <string>();
        List <string>      channelCloseMethodsCalled = new List <string>();
        List <string>      factoryOpenMethodsCalled  = new List <string>();
        List <string>      factoryCloseMethodsCalled = new List <string>();
        string             testMessageBody           = "CustomChannelTest_Async";
        Message            inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory and intercept all open and close method calls
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            MockCommunicationObject.InterceptAllOpenMethods(mockChannelFactory, factoryOpenMethodsCalled);
            MockCommunicationObject.InterceptAllCloseMethods(mockChannelFactory, factoryCloseMethodsCalled);

            // Override the OnCreateChannel call so we get the mock channel created by the factory
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the default mock channel and intercept all its open and close method calls
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                MockCommunicationObject.InterceptAllOpenMethods(mockRequestChannel, channelOpenMethodsCalled);
                MockCommunicationObject.InterceptAllCloseMethods(mockRequestChannel, channelCloseMethodsCalled);

                return(mockRequestChannel);
            };
            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Explicitly open factory asynchronously because the implicit open is synchronous
        IAsyncResult openResult = factory.BeginOpen(null, null);
        Task         openTask   = Task.Factory.FromAsync(openResult, factory.EndOpen);

        openTask.GetAwaiter().GetResult();

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Task <Message> processTask = channel.ProcessAsync(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        Message outputMessage = processTask.GetAwaiter().GetResult();
        var     result        = outputMessage.GetBody <string>();

        // Explicitly close the channel factory asynchronously.
        // One of the important aspects of this test is that an asynchronous
        // close of the factory also asynchronously closes the channel.
        IAsyncResult asyncResult = factory.BeginClose(null, null);
        Task         task        = Task.Factory.FromAsync(asyncResult, factory.EndClose);

        task.GetAwaiter().GetResult();

        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        string expectedOpens  = "OnOpening,OnBeginOpen,OnOpened";
        string expectedCloses = "OnClosing,OnBeginClose,OnClosed";

        string actualOpens = String.Join(",", channelOpenMethodsCalled);

        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected channel open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        string actualCloses = String.Join(",", channelCloseMethodsCalled);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected channel close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        actualOpens = String.Join(",", factoryOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected factory open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        actualCloses = String.Join(",", factoryCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected factory close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        Assert.True(factory.State == CommunicationState.Closed,
                    String.Format("Expected factory's final state to be Closed but was '{0}'", factory.State));

        Assert.True(((ICommunicationObject)channel).State == CommunicationState.Closed,
                    String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));
    }
Пример #14
0
    public static void CustomChannel_ChannelInitializer_CreateChannel_Calls_Initialize()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to observe whether
        // its method is invoked.
        bool initializeCalled = false;
        MockChannelInitializer mockInitializer = new MockChannelInitializer();
        mockInitializer.InitializeOverride = (chnl) =>
        {
            initializeCalled = true;
            mockInitializer.DefaultInitialize(chnl);
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        factory.CreateChannel();

        // *** VALIDATE *** \\
        Assert.True(initializeCalled, "Initialize was not called.");
    }
Пример #15
0
    public static void CustomChannel_ChannelInitializer_Failed_Initialize_Throws()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to fault inject an exception
        InvalidOperationException thrownException = new InvalidOperationException("test threw this");
        MockChannelInitializer mockInitializer = new MockChannelInitializer();
        mockInitializer.InitializeOverride = (chnl) =>
        {
            mockInitializer.DefaultInitialize(chnl);
            throw thrownException;
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        InvalidOperationException caughtException =
            Assert.Throws<InvalidOperationException>(() => factory.CreateChannel());

        // *** VALIDATE *** \\
        Assert.True(String.Equals(caughtException.Message, thrownException.Message), 
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                    thrownException.Message, caughtException.Message));
    }
Пример #16
0
    public static void CustomChannel_InteractiveInitializer_Can_Be_Blocked()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to observe whether
        // its methods are invoked.
        bool beginInitializeCalled = false;
        bool endInitializeCalled = false;
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();
        mockInitializer.BeginDisplayInitializationUIOverride = (chnl, callback, state) =>
        {
            beginInitializeCalled = true;
            return mockInitializer.DefaultBeginDisplayInitializationUI(chnl, callback, state);
        };

        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            endInitializeCalled = true;
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // block attempts to use interactive UI
        ((IClientChannel)channel).AllowInitializationUI = false;

        // *** EXECUTE *** \\
        Assert.Throws<InvalidOperationException>(() => ((IClientChannel)channel).DisplayInitializationUI());

        // *** VALIDATE *** \\
        Assert.False(beginInitializeCalled, "BeginDisplayInitializationUI should not have been called.");
        Assert.False(endInitializeCalled, "EndDisplayInitializationUI should not have been called.");
    }
Пример #17
0
    public static void CustomChannel_Async_Request_Exception_Propagates()
    {
        MockChannelFactory<IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel = null;
        string testMessageBody = "CustomChannelTest_Async";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);
        string expectedExceptionMessage = "Async exception message";

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func<Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory so we can intercept channel creation
            mockChannelFactory = new MockChannelFactory<IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());

            // Override the OnCreateChannel call so we can fault inject an exception
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the mock channel and fault inject an exception in the EndRequest
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                mockRequestChannel.EndRequestOverride = (IAsyncResult ar) =>
                {
                    throw new InvalidOperationException(expectedExceptionMessage);
                };
                return mockRequestChannel;
            };
            return mockChannelFactory;
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);
        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        // The ProcessAsync should not throw -- the fault comes in the EndRequest
        Task<Message> processTask = channel.ProcessAsync(inputMessage);

        InvalidOperationException actualException = Assert.Throws<InvalidOperationException>(() =>
        {
            // awaiting the task will invoke the EndRequest
            processTask.GetAwaiter().GetResult();
        });

        // *** VALIDATE *** \\
        Assert.True(String.Equals(expectedExceptionMessage, actualException.Message),
                    String.Format("Expected exception message to be '{0}' but actual was '{1}'",
                                  expectedExceptionMessage, actualException.Message));
    }
Пример #18
0
    public static void CustomChannel_InteractiveInitializer_Error_Blocks_Open()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to throw during initialization
        InvalidOperationException thrownException = new InvalidOperationException("ui initialization failed");
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();
        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            // Throw from the EndDisplayInitializationUI call to simulate a UI
            // action that throws an error to prevent the open.
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
            throw thrownException;
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        // The implicit open does the call to display interactive UI.
        InvalidOperationException caughtException = 
            Assert.Throws<InvalidOperationException>(() => channel.Process(inputMessage));

        // *** VALIDATE *** \\
        Assert.True(string.Equals(thrownException.Message, caughtException.Message),
                    String.Format("Expected exception message to be '{0}' but actual was '{1}'",
                                  thrownException.Message, caughtException.Message));
    }