Example #1
0
    public static void ClientBaseOfT_Dispose_Closes_Factory_And_Proxy()
    {
        MyClientBaseWithChannelBase  client                   = null;
        IWcfServiceBeginEndGenerated serviceProxy             = null;
        ChannelFactory <IWcfServiceBeginEndGenerated> factory = null;

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

            string endpoint = Endpoints.HttpSoap12_Address;
            client  = new MyClientBaseWithChannelBase(customBinding, new EndpointAddress(endpoint));
            factory = client.ChannelFactory;

            // *** EXECUTE *** \\
            // Explicitly open the ClientBase to follow general WCF guidelines
            ((ICommunicationObject)client).Open();

            // Use the internal proxy generated by ClientBase to most resemble how svcutil-generated code works.
            // Customers cannot normally access this protected member explicitly, but the generated code uses it.
            serviceProxy = client.Proxy;

            // *** EXECUTE *** \\
            // ClientBase is IDisposable, which should close the client, factory and proxy
            ((IDisposable)client).Dispose();

            // *** VALIDATE *** \\
            // Closing the ClientBase closes the internal channel and factory
            Assert.True(CommunicationState.Closed == client.State,
                        String.Format("Expected client state to be Closed but actual was '{0}'", client.State));

            // Closing the ClientBase also closes the internal channel
            Assert.True(CommunicationState.Closed == ((ICommunicationObject)serviceProxy).State,
                        String.Format("Expected proxy state to be Closed but actual was '{0}'", ((ICommunicationObject)serviceProxy).State));

            // Closing the ClientBase also closes the channel factory
            Assert.True(CommunicationState.Closed == factory.State,
                        String.Format("Expected channel factory state to be Closed but actual was '{0}'", factory.State));

            // *** CLEANUP *** \\
            // No further cleanup is needed because the EXECUTE portion accomplished that.
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, client, factory);
        }
    }
Example #2
0
    public static void ChannelBaseOfT_Async_Open_Close_Events_Fire()
    {
        MyClientBaseWithChannelBase client        = null;
        List <string> eventsCalled                = new List <string>(4);
        List <string> proxyEventsCalled           = new List <string>(4);
        IWcfServiceBeginEndGenerated serviceProxy = null;

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

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

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

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

            // *** EXECUTE *** \\
            IAsyncResult ar     = serviceProxy.BeginEcho("Hello", callback: null, asyncState: null);
            string       result = serviceProxy.EndEcho(ar);

            ar = ((ICommunicationObject)client).BeginClose(null, null);
            ((ICommunicationObject)client).EndClose(ar);

            // *** 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 *** \\
            // No further cleanup is needed because the EXECUTE portion accomplished that.
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, (ICommunicationObject)client);
        }
    }
    private static void ServiceContract_TypedProxy_AsyncBeginEnd_Call_TestImpl(Binding binding, string endpoint, string testName)
    {
        // Verifies a typed proxy can call a service operation asynchronously using Begin/End
        ChannelFactory <IWcfServiceBeginEndGenerated> factory = null;
        EndpointAddress endpointAddress           = null;
        IWcfServiceBeginEndGenerated serviceProxy = null;
        ManualResetEvent             waitEvent    = null;
        string result = null;

        try
        {
            // *** SETUP *** \\
            endpointAddress = new EndpointAddress(endpoint);
            factory         = new ChannelFactory <IWcfServiceBeginEndGenerated>(binding, endpointAddress);
            serviceProxy    = factory.CreateChannel();
            waitEvent       = new ManualResetEvent(false);

            // *** EXECUTE *** \\
            // The callback is optional with this Begin call, but we want to test that it works.
            // This delegate should execute when the call has completed, and that is how it gets the result of the call.
            AsyncCallback callback = (iar) =>
            {
                result = serviceProxy.EndEcho(iar);
                waitEvent.Set();
            };

            IAsyncResult ar = serviceProxy.BeginEcho("Hello", callback, null);

            // *** VALIDATE *** \\
            // This test requires the callback to be called.
            // An actual timeout should call the callback, but we still set
            // a maximum wait time in case that does not happen.
            bool success = waitEvent.WaitOne(ScenarioTestHelpers.TestTimeout);
            Assert.True(success, String.Format("The AsyncCallback was not called. If the AsyncCallback had been called the waitEvent would have been set to 'True', but the value of waitEvent was: {0}", success));
            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);
        }
    }
Example #4
0
    private static void ServiceContract_TypedProxy_AsyncBeginEnd_Call(Binding binding, string endpoint, string testName)
    {
        // Verifies a typed proxy can call a service operation asynchronously using Begin/End
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            ChannelFactory <IWcfServiceBeginEndGenerated> factory = new ChannelFactory <IWcfServiceBeginEndGenerated>(binding, new EndpointAddress(endpoint));
            IWcfServiceBeginEndGenerated serviceProxy             = factory.CreateChannel();
            string           result    = null;
            ManualResetEvent waitEvent = new ManualResetEvent(false);

            // The callback is optional with this Begin call, but we want to test that it works.
            // This delegate should execute when the call has completed, and that is how it gets the result of the call.
            AsyncCallback callback = (iar) =>
            {
                result = serviceProxy.EndEcho(iar);
                waitEvent.Set();
            };

            IAsyncResult ar = serviceProxy.BeginEcho("Hello", callback, null);

            // This test requires the callback to be called.
            // An actual timeout should call the callback, but we still set
            // a maximum wait time in case that does not happen.
            bool success = waitEvent.WaitOne(ScenarioTestHelpers.TestTimeout);
            if (!success)
            {
                errorBuilder.AppendLine("AsyncCallback was not called.");
            }

            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: {0} FAILED with the following errors: {1}", testName, errorBuilder));
    }
    private static void ServiceContract_TypedProxy_AsyncBeginEnd_Call_WithNoCallback_TestImpl(Binding binding, string endpoint, string testName)
    {
        // This test verifies a typed proxy can call a service operation asynchronously using Begin/End
        ChannelFactory <IWcfServiceBeginEndGenerated> factory = null;
        EndpointAddress endpointAddress           = null;
        IWcfServiceBeginEndGenerated serviceProxy = null;
        string result = null;

        try
        {
            // *** SETUP *** \\
            endpointAddress = new EndpointAddress(endpoint);
            factory         = new ChannelFactory <IWcfServiceBeginEndGenerated>(binding, endpointAddress);
            serviceProxy    = factory.CreateChannel();

            // *** EXECUTE *** \\
            IAsyncResult ar = serviceProxy.BeginEcho("Hello", null, null);
            // An actual timeout should complete the ar, but we still set
            // a maximum wait time in case that does not happen.
            bool success = ar.AsyncWaitHandle.WaitOne(ScenarioTestHelpers.TestTimeout);

            // *** VALIDATE *** \\
            Assert.True(success, String.Format("The IAsyncResult was not called. If the IAsyncResult had been called the AsyncWaitHandle would have been set to 'True', but the value of AsyncWaitHandle was: {0}", success));

            // *** EXECUTE *** \\
            result = serviceProxy.EndEcho(ar);

            // *** 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);
        }
    }
Example #6
0
    private static void ServiceContract_TypedProxy_AsyncBeginEnd_Call_WithNoCallback(Binding binding, string endpoint, string testName)
    {
        // This test verifies a typed proxy can call a service operation asynchronously using Begin/End
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            ChannelFactory <IWcfServiceBeginEndGenerated> factory = new ChannelFactory <IWcfServiceBeginEndGenerated>(binding, new EndpointAddress(endpoint));
            IWcfServiceBeginEndGenerated serviceProxy             = factory.CreateChannel();
            string result = null;

            IAsyncResult ar = serviceProxy.BeginEcho("Hello", null, null);
            // An actual timeout should complete the ar, but we still set
            // a maximum wait time in case that does not happen.
            bool success = ar.AsyncWaitHandle.WaitOne(ScenarioTestHelpers.TestTimeout);
            if (success)
            {
                result = serviceProxy.EndEcho(ar);
            }
            else
            {
                errorBuilder.AppendLine("AsyncCallback was not called.");
            }

            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: {0} FAILED with the following errors: {1}", testName, errorBuilder));
    }
Example #7
0
    public static void ChannelBaseOfT_Asynchronous_RoundTrip()
    {
        // This test verifies ClientBase<T> can be used to create a custom ChannelBase<T> proxy and invoke an operation over Http

        MyClientBaseWithChannelBase  client       = null;
        IWcfServiceBeginEndGenerated serviceProxy = null;
        string echoText = "Hello";

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

            client       = new MyClientBaseWithChannelBase(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address));
            serviceProxy = client.Proxy;

            // *** EXECUTE *** \\
            // Note: public contract supports only async use on ChannelBase<T>
            IAsyncResult ar     = serviceProxy.BeginEcho(echoText, callback: null, asyncState: null);
            string       result = serviceProxy.EndEcho(ar);

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

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