Ejemplo n.º 1
0
    public static void ClientBaseOfT_Sync_RoundTrip_Check_CommunicationState()
    {
        CustomBinding customBinding = new CustomBinding();

        customBinding.Elements.Add(new TextMessageEncodingBindingElement());
        customBinding.Elements.Add(new HttpTransportBindingElement());

        MyClientBase client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.HttpSoap12_Address));

        Assert.Equal(CommunicationState.Created, client.State);

        IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel();

        Assert.Equal(CommunicationState.Opened, client.State);

        try
        {
            string result = serviceProxy.Echo("Hello");
            Assert.Equal(CommunicationState.Opened, client.State);

            ((ICommunicationObject)client).Close();
            Assert.Equal(CommunicationState.Closed, client.State);
        }
        finally
        {
            // normally we'd also check for if (client != null && client.State != CommuncationState.Closed),
            // but this is a test and it'd be good to have the Abort happen and the channel is still Closed
            if (client != null)
            {
                client.Abort();
                Assert.Equal(CommunicationState.Closed, client.State);
            }
        }
    }
Ejemplo n.º 2
0
    public static void ServiceContract_TypedProxy_Synchronous_Call()
    {
        // This test verifies a typed proxy can call a service operation synchronously
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            CustomBinding customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());

            // Note the service interface used.  It was manually generated with svcutil.
            ChannelFactory <IWcfServiceGenerated> factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address));
            IWcfServiceGenerated serviceProxy             = factory.CreateChannel();

            string result = serviceProxy.Echo("Hello");
            if (!string.Equals(result, "Hello"))
            {
                errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result));
            }

            factory.Close();
        }
        catch (Exception ex)
        {
            errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString()));
        }

        Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: TypedProxySynchronousCall FAILED with the following errors: {0}", errorBuilder));
    }
Ejemplo n.º 3
0
    public static void ClientBaseOfT_Sync_RoundTrip_Call_Using_NetTcpTransport()
    {
        // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation over Tcp
        // (request reply over Tcp)

        // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation over Http

        CustomBinding binding = new CustomBinding(
            new TextMessageEncodingBindingElement(),
            new TcpTransportBindingElement());

        MyClientBase         client       = new MyClientBase(binding, new EndpointAddress(Endpoints.Tcp_CustomBinding_NoSecurity_Text_Address));
        IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel();

        try
        {
            string result = serviceProxy.Echo("Hello");
            Assert.Equal("Hello", result);
        }
        finally
        {
            if (client != null && client.State != CommunicationState.Closed)
            {
                client.Abort();
            }
        }
    }
Ejemplo n.º 4
0
    public static void ClientBaseOfT_Call()
    {
        // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            CustomBinding customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());

            MyClientBase         client       = new MyClientBase(customBinding, new EndpointAddress(BaseAddress.HttpBaseAddress));
            IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel();

            string result = serviceProxy.Echo("Hello");
            if (!string.Equals(result, "Hello"))
            {
                errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result));
            }
        }
        catch (Exception ex)
        {
            errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString()));
        }

        Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: ClientBaseOfTCall FAILED with the following errors: {0}", errorBuilder));
    }
Ejemplo n.º 5
0
    public static void ClientBaseOfT_Sync_RoundTrip_Call_Using_NetTcpTransport()
    {
        // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation over Tcp
        // (request reply over Tcp)

        MyClientBase         client       = null;
        IWcfServiceGenerated serviceProxy = null;

        try
        {
            // *** SETUP *** \\
            CustomBinding binding = new CustomBinding(new TextMessageEncodingBindingElement(), new TcpTransportBindingElement());

            client       = new MyClientBase(binding, new EndpointAddress(Endpoints.Tcp_CustomBinding_NoSecurity_Text_Address));
            serviceProxy = client.ChannelFactory.CreateChannel();

            // *** EXECUTE *** \\
            string result = serviceProxy.Echo("Hello");

            // *** VALIDATE *** \\
            Assert.Equal("Hello", result);

            // *** CLEANUP *** \\
            ((ICommunicationObject)client).Close();
            ((ICommunicationObject)serviceProxy).Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, (ICommunicationObject)client);
        }
    }
Ejemplo n.º 6
0
    public static void ClientMessageInspector_Verify_Invoke()
    {
        // This test verifies ClientMessageInspector can be added to the client endpoint behaviors
        // and this is it called properly when a message is sent.
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            CustomBinding customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());

            MyClientBase client = new MyClientBase(customBinding, new EndpointAddress(BaseAddress.HttpBaseAddress));

            // Add the ClientMessageInspector and give it an instance where it can record what happens when it is called.
            ClientMessageInspectorData data = new ClientMessageInspectorData();
            client.Endpoint.EndpointBehaviors.Add(new ClientMessageInspectorBehavior(data));
            IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel();

            // This proxy call should invoke the client message inspector
            string result = serviceProxy.Echo("Hello");
            if (!string.Equals(result, "Hello"))
            {
                errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result));
            }

            if (!data.BeforeSendRequestCalled)
            {
                errorBuilder.AppendLine(String.Format("Did not call BeforeSendRequest"));
            }

            if (data.Request == null)
            {
                errorBuilder.AppendLine(String.Format("Did not call pass Request to BeforeSendRequest"));
            }

            if (data.Channel == null)
            {
                errorBuilder.AppendLine(String.Format("Did not call pass Channel to BeforeSendRequest"));
            }

            if (!data.AfterReceiveReplyCalled)
            {
                errorBuilder.AppendLine(String.Format("Did not call AfterReceiveReplyCalled"));
            }

            if (data.Reply == null)
            {
                errorBuilder.AppendLine(String.Format("Did not call pass Reply to AfterReceiveReplyCalled"));
            }
        }
        catch (Exception ex)
        {
            errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString()));
        }

        Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: ClientMessageInspectorScenario FAILED with the following errors: {0}", errorBuilder));
    }
Ejemplo n.º 7
0
    public static void ClientBaseOfT_Sync_Open_Close_Events_Fire()
    {
        MyClientBase         client            = null;
        List <string>        eventsCalled      = new List <string>(4);
        List <string>        proxyEventsCalled = new List <string>(4);
        IWcfServiceGenerated proxy             = null;

        try
        {
            // *** SETUP *** \\
            CustomBinding customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());

            client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.HttpSoap12_Address));

            // Listen to all events on the ClientBase and on the generated proxy
            ClientBaseTestHelpers.RegisterForEvents(client, eventsCalled);

            proxy = client.Proxy;
            ClientBaseTestHelpers.RegisterForEvents((ICommunicationObject)proxy, proxyEventsCalled);

            // *** EXECUTE *** \\
            proxy.Echo("Hello");
            ((ICommunicationObject)client).Close();

            // *** VALIDATE *** \\

            // We expect both the ClientBase and the generated proxy to have fired all the open/close events
            string expected = "Opening,Opened,Closing,Closed";
            string actual   = String.Join(",", eventsCalled);

            Assert.True(String.Equals(expected, actual),
                        String.Format("Expected client to receive events '{0}' but actual was '{1}'", expected, actual));

            actual = String.Join(",", proxyEventsCalled);
            Assert.True(String.Equals(expected, actual),
                        String.Format("Expected proxy to receive events '{0}' but actual was '{1}'", expected, actual));

            // *** CLEANUP *** \\
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)proxy, (ICommunicationObject)client);
        }
    }
Ejemplo n.º 8
0
    public static void ClientBaseOfT_Sync_Removed_Open_Close_Events_Do_Not_Fire()
    {
        MyClientBase         client            = null;
        List <string>        eventsCalled      = new List <string>(4);
        List <string>        proxyEventsCalled = new List <string>(4);
        IWcfServiceGenerated proxy             = null;

        try
        {
            // *** SETUP *** \\
            CustomBinding customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());

            client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.HttpSoap12_Address));

            // Both Add and Remove event handlers
            RegisterForEvents(client, eventsCalled, deregister: true);

            proxy = client.Proxy;
            RegisterForEvents((ICommunicationObject)proxy, proxyEventsCalled, deregister: true);

            // *** EXECUTE *** \\
            proxy.Echo("Hello");
            ((ICommunicationObject)client).Close();

            // *** VALIDATE *** \\

            // We expect both the ClientBase and the generated proxy to have NOT fired all the open/close events
            string actual = String.Join(",", eventsCalled);

            Assert.True(eventsCalled.Count == 0,
                        String.Format("Expected client NOT to receive events but actual was '{0}'", actual));

            actual = String.Join(",", proxyEventsCalled);
            Assert.True(proxyEventsCalled.Count == 0,
                        String.Format("Expected proxy NOT to receive events but actual was '{0}'", actual));

            // *** CLEANUP *** \\
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)proxy, (ICommunicationObject)client);
        }
    }
Ejemplo n.º 9
0
    public static void ClientMessageInspector_Verify_Invoke()
    {
        // This test verifies ClientMessageInspector can be added to the client endpoint behaviors
        // and this is it called properly when a message is sent.

        MyClientBase         client       = null;
        IWcfServiceGenerated serviceProxy = null;

        try
        {
            // *** SETUP *** \\
            CustomBinding customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());

            client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address));

            // Add the ClientMessageInspector and give it an instance where it can record what happens when it is called.
            ClientMessageInspectorData data = new ClientMessageInspectorData();
            client.Endpoint.EndpointBehaviors.Add(new ClientMessageInspectorBehavior(data));

            serviceProxy = client.ChannelFactory.CreateChannel();

            // *** EXECUTE *** \\
            // This proxy call should invoke the client message inspector
            string result = serviceProxy.Echo("Hello");

            // *** VALIDATE *** \\
            Assert.Equal("Hello", result);
            Assert.True(data.BeforeSendRequestCalled, "BeforeSendRequest should have been called");
            Assert.True(data.Request != null, "Did not call pass Request to BeforeSendRequest");
            Assert.True(data.Channel != null, "Did not call pass Channel to BeforeSendRequest");
            Assert.True(data.AfterReceiveReplyCalled, "AfterReceiveReplyCalled should have been called");
            Assert.True(data.Reply != null, "Did not call pass Reply to AfterReceiveReplyCalled");

            // *** CLEANUP *** \\
            ((ICommunicationObject)client).Close();
            ((ICommunicationObject)serviceProxy).Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, (ICommunicationObject)client);
        }
    }
    public static void ServiceContract_TypedProxy_Synchronous_Call()
    {
        // This test verifies a typed proxy can call a service operation synchronously
        CustomBinding customBinding = null;
        ChannelFactory <IWcfServiceGenerated> factory = null;
        EndpointAddress      endpointAddress          = null;
        IWcfServiceGenerated serviceProxy             = null;
        string result = null;

        try
        {
            // *** SETUP *** \\
            customBinding = new CustomBinding();
            customBinding.Elements.Add(new TextMessageEncodingBindingElement());
            customBinding.Elements.Add(new HttpTransportBindingElement());
            endpointAddress = new EndpointAddress(Endpoints.DefaultCustomHttp_Address);
            // Note the service interface used.  It was manually generated with svcutil.
            factory      = new ChannelFactory <IWcfServiceGenerated>(customBinding, endpointAddress);
            serviceProxy = factory.CreateChannel();

            // *** EXECUTE *** \\
            result = serviceProxy.Echo("Hello");

            // *** VALIDATE *** \\
            Assert.True(String.Equals(result, "Hello"), String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result));

            // *** CLEANUP *** \\
            factory.Close();
            ((ICommunicationObject)serviceProxy).Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
        }
    }