Ejemplo n.º 1
0
    public static void OperationContextScope_HttpRequestCustomMessageHeader_RoundTrip_Verify()
    {
        string customHeaderName  = "OperationContextScopeCustomHeader";
        string customHeaderNS    = "http://tempuri.org/OperationContextScope_HttpRequestCustomMessageHeader_RoundTrip_Verify";
        string customHeaderValue = "CustomHappyValue";

        MyClientBase <IWcfService> client = null;
        IWcfService serviceProxy          = null;

        try
        {
            // *** SETUP *** \\
            BasicHttpBinding binding = new BasicHttpBinding();

            client       = new MyClientBase <IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic));
            serviceProxy = client.ChannelFactory.CreateChannel();

            using (OperationContextScope scope = new OperationContextScope((IContextChannel)serviceProxy))
            {
                MessageHeader header
                    = MessageHeader.CreateHeader(
                          customHeaderName,
                          customHeaderNS,
                          customHeaderValue
                          );
                OperationContext.Current.OutgoingMessageHeaders.Add(header);

                // *** EXECUTE *** \\
                Dictionary <string, string> incomingMessageHeaders = serviceProxy.GetIncomingMessageHeaders();
                string result = ClientBaseTestHelpers.GetHeader(customHeaderName, customHeaderNS, incomingMessageHeaders);

                // *** VALIDATE *** \\
                Assert.Equal(customHeaderValue, result);
            }

            // *** EXECUTE *** \\
            //Call outside of scope should not have the custom header
            Dictionary <string, string> outofScopeIncomingMessageHeaders = serviceProxy.GetIncomingMessageHeaders();
            string outofScopeResult = ClientBaseTestHelpers.GetHeader(customHeaderName, customHeaderNS, outofScopeIncomingMessageHeaders);

            // *** VALIDATE *** \\
            Assert.True(string.Empty == outofScopeResult, string.Format("Expect call out of the OperationContextScope does not have the custom header {0}", customHeaderName));

            // *** CLEANUP *** \\
            ((ICommunicationObject)client).Close();
            ((ICommunicationObject)serviceProxy).Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, (ICommunicationObject)client);
        }
    }
Ejemplo n.º 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);
        }
    }
Ejemplo n.º 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);
        }
    }
Ejemplo n.º 4
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
            ClientBaseTestHelpers.RegisterForEvents(client, eventsCalled, deregister: true);

            proxy = client.Proxy;
            ClientBaseTestHelpers.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);
        }
    }