Example #1
0
        public void Task_UseWithOperationContextScope_Expected_OperationPerformed()
        {
            var channelFactory = new AsyncChannelFactory <IDerivedTestService>();
            var channel        = channelFactory.CreateChannel();

            var userContextHeaderName      = "UserContext";
            var userContextHeaderNamespace = "http://my.context.namespace/";
            var userContextHeaderValue     = "My user context";

            var testPart = new MultipartComplete(2, TestComplete);

            using (var scope = new OperationContextScope(channel.ToContextChannel()))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        userContextHeaderName,
                        userContextHeaderNamespace,
                        userContextHeaderValue));

                channel.ExecuteTask(ws => ws.ReadHeaderOperation())
                .ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        Assert.Fail();
                    }
                    var result = t.Result;
                    Assert.AreEqual(userContextHeaderValue, result);
                    testPart.Completed();
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }

            channel.ExecuteTask(ws => ws.ReadHeaderOperation())
            .ContinueWith(
                t =>
            {
                if (t.IsFaulted)
                {
                    Assert.Fail();
                }
                var result = t.Result;
                Assert.IsNull(result);
                testPart.Completed();
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Example #2
0
        public void UseWithOperationContextScope_Expected_OperationPerformed()
        {
            var channelFactory = new AsyncChannelFactory <IDerivedTestService>();
            var channel        = channelFactory.CreateChannel();

            var userContextHeaderName      = "UserContext";
            var userContextHeaderNamespace = "http://my.context.namespace/";
            var userContextHeaderValue     = "My user context";

            var testPart = new MultipartComplete(2, TestComplete);

            using (var scope = new OperationContextScope(channel.ToContextChannel()))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        userContextHeaderName,
                        userContextHeaderNamespace,
                        userContextHeaderValue));

                channel.ExecuteAsync(
                    ws => ws.ReadHeaderOperation(),
                    result =>
                {
                    Assert.AreEqual(userContextHeaderValue, result);
                    testPart.Completed();
                },
                    exception => { Assert.Fail(); });
            }

            channel.ExecuteAsync(
                ws => ws.ReadHeaderOperation(),
                result =>
            {
                Assert.IsNull(result);
                testPart.Completed();
            },
                exception => { Assert.Fail(); });
        }