Esempio 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);
        }
    }
Esempio n. 2
0
    public static void OperationContextScope_HttpRequestCustomMessageHeader_RoundTrip_Verify()
    {
        BasicHttpBinding           binding = new BasicHttpBinding();
        MyClientBase <IWcfService> client  = new MyClientBase <IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic));
        IWcfService serviceProxy           = client.ChannelFactory.CreateChannel();

        string customHeaderName  = "OperationContextScopeCustomHeader";
        string customHeaderNS    = "http://tempuri.org/OperationContextScope_HttpRequestCustomMessageHeader_RoundTrip_Verify";
        string customHeaderValue = "CustomHappyValue";

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

                Dictionary <string, string> incomingMessageHeaders = serviceProxy.GetIncomingMessageHeaders();
                string result = GetHeader(customHeaderName, customHeaderNS, incomingMessageHeaders);

                Assert.Equal(customHeaderValue, result);
            }

            //Call outside of scope should not have the custom header
            Dictionary <string, string> outofScopeIncomingMessageHeaders = serviceProxy.GetIncomingMessageHeaders();
            string outofScopeResult = GetHeader(customHeaderName, customHeaderNS, outofScopeIncomingMessageHeaders);
            Assert.True(string.Empty == outofScopeResult, string.Format("Expect call out of the OperationContextScope does not have the custom header {0}", customHeaderName));
            ((ICommunicationObject)client).Close();
        }
        finally
        {
            if (client != null && client.State != CommunicationState.Closed)
            {
                client.Abort();
            }
        }
    }