예제 #1
0
    private static void ServiceContract_TypedProxy_AsyncTask_Call(Binding binding, string endpoint, string testName)
    {
        // This test verifies a typed proxy can call a service operation asynchronously using Task<string>
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            ChannelFactory <IWcfServiceGenerated> factory = new ChannelFactory <IWcfServiceGenerated>(binding, new EndpointAddress(endpoint));
            IWcfServiceGenerated serviceProxy             = factory.CreateChannel();

            Task <string> task   = serviceProxy.EchoAsync("Hello");
            string        result = task.Result;
            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: TypedProxyAsyncTaskCall FAILED with the following errors: {0}", errorBuilder));
    }
    private static void ServiceContract_TypedProxy_AsyncTask_Call_TestImpl(Binding binding, string endpoint, string testName)
    {
        // This test verifies a typed proxy can call a service operation asynchronously using Task<string>
        ChannelFactory <IWcfServiceGenerated> factory = null;
        EndpointAddress      endpointAddress          = null;
        IWcfServiceGenerated serviceProxy             = null;
        string result = null;

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

            // *** EXECUTE *** \\
            Task <string> task = serviceProxy.EchoAsync("Hello");
            result = task.Result;

            // *** 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);
        }
    }
예제 #3
0
    public static void ClientBaseOfT_Async_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 *** \\
            Task <string> task = proxy.EchoAsync("Hello");
            task.GetAwaiter().GetResult();

            IAsyncResult 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 *** \\
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)proxy, (ICommunicationObject)client);
        }
    }
예제 #4
0
    public static void ServiceContract_TypedProxy_Task_Call_WithSyncContext_ContinuesOnSameThread()
    {
        // This test verifies a task based call to a service operation continues on the same thread
        StringBuilder errorBuilder = new StringBuilder();

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

            ChannelFactory <IWcfServiceGenerated> factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address));
            IWcfServiceGenerated serviceProxy             = factory.CreateChannel();
            string result = String.Empty;

            bool success = Task.Run(() =>
            {
                SingleThreadSynchronizationContext.Run(async delegate
                {
                    int startThread = Environment.CurrentManagedThreadId;
                    result          = await serviceProxy.EchoAsync("Hello");
                    if (startThread != Environment.CurrentManagedThreadId)
                    {
                        errorBuilder.AppendLine(String.Format("Expected continuation to happen on thread {0} but actually continued on thread {1}",
                                                              startThread, Environment.CurrentManagedThreadId));
                    }
                });
            }).Wait(ScenarioTestHelpers.TestTimeout);

            if (!success)
            {
                errorBuilder.AppendLine(String.Format("Test didn't complete within the expected time"));
            }

            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: TaskCallWithSynchContextContinuesOnSameThread FAILED with the following errors: {0}", errorBuilder));
    }
    public static void ServiceContract_TypedProxy_Task_Call_WithSyncContext_ContinuesOnSameThread()
    {
        // This test verifies a task based call to a service operation continues on the same thread
        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);
            factory         = new ChannelFactory <IWcfServiceGenerated>(customBinding, endpointAddress);
            serviceProxy    = factory.CreateChannel();

            // *** EXECUTE *** \\
            bool success = Task.Run(() =>
            {
                SingleThreadSynchronizationContext.Run(async delegate
                {
                    int startThread = Environment.CurrentManagedThreadId;
                    result          = await serviceProxy.EchoAsync("Hello");

                    Assert.True(startThread == Environment.CurrentManagedThreadId, String.Format("Expected continuation to happen on thread {0} but actually continued on thread {1}",
                                                                                                 startThread, Environment.CurrentManagedThreadId));
                });
            }).Wait(ScenarioTestHelpers.TestTimeout);

            // *** VALIDATE *** \\
            Assert.True(success, String.Format("Test didn't complete within the expected time"));
            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);
        }
    }