public static void InstanceContextMode_PerCall()
        {
            PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts();
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <PerCallInstanceContextSimpleServiceAndBehavior, ISimpleService>(
                (services) =>
            {
                services.AddTransient <PerCallInstanceContextSimpleServiceAndBehavior>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            // Instance created as part of service startup to probe if type is availale in DI
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount);
            // Instance not disposed as it implements IServiceBehavior and is added to service behaviors
            Assert.Equal(0, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount);
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.AddBindingParametersCallCount);
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.ApplyDispatchBehaviorCount);
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.ValidateCallCount);

            PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts();
            string echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            PerCallInstanceContextSimpleServiceAndBehavior.WaitForDisposalCount(2, TimeSpan.FromSeconds(30));
            Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount);
            Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InstanceContextMode_Single()
        {
            SingleInstanceContextSimpleService.ClearCounts();
            var serviceInstance = new SingleInstanceContextSimpleService();

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleInstanceContextSimpleService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(serviceInstance);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            Assert.Equal(1, SingleInstanceContextSimpleService.AddBindingParametersCallCount);
            Assert.Equal(1, SingleInstanceContextSimpleService.ApplyDispatchBehaviorCount);
            Assert.Equal(1, SingleInstanceContextSimpleService.ValidateCallCount);
            string echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            Assert.Equal(1, SingleInstanceContextSimpleService.CreationCount);
            Assert.Equal(0, SingleInstanceContextSimpleService.DisposalCount);
            Assert.Equal(2, serviceInstance.CallCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #3
0
        public void ServiceProviderShouldBeExposedThroughOperationContextInstanceContextExtensionsWhenSingleServiceIsRegisteredWithinDI()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleSimpleServiceUsingServiceProviderFromOperationContext, ISimpleService>(
                (services) =>
            {
                services.AddTransient <SingleSimpleServiceUsingServiceProviderFromOperationContext>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            channel.Echo(input);
            channel.Echo(input);

            factory.Close();
        }
예제 #4
0
        public static void ServiceThrowsExceptionDetailsIncludedInFault()
        {
            string exceptionMessage    = "This is the exception message";
            string stackTraceTopMethod = "   at ErrorHandling.ThrowingService.Echo(String echo)";

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <ThrowingDetailInFaultService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(new ThrowingDetailInFaultService(new Exception(exceptionMessage)));
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.IClientChannel)channel).Open();
            System.ServiceModel.FaultException <System.ServiceModel.ExceptionDetail> exceptionThrown = Assert.Throws <System.ServiceModel.FaultException <System.ServiceModel.ExceptionDetail> >(() =>
            {
                _ = channel.Echo("hello");
            });
            Assert.NotNull(exceptionThrown);
            Assert.NotNull(exceptionThrown.Detail);
            Assert.True(exceptionThrown.Code.IsReceiverFault);
            System.ServiceModel.ExceptionDetail detail = exceptionThrown.Detail;
            Assert.Equal(exceptionMessage, detail.Message);
            Assert.StartsWith(stackTraceTopMethod, detail.StackTrace);
            Assert.Equal("InternalServiceFault", exceptionThrown.Code.SubCode.Name);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #5
0
        public static void ServiceThrowsTimeoutException()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <ThrowingService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(new ThrowingService(new TimeoutException()));
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            System.ServiceModel.FaultException exceptionThrown = null;
            try
            {
                string echo = channel.Echo("hello");
            }
            catch (System.ServiceModel.FaultException e)
            {
                exceptionThrown = e;
            }

            Assert.NotNull(exceptionThrown);
            Assert.True(exceptionThrown.Code.IsReceiverFault);
            Assert.Equal("InternalServiceFault", exceptionThrown.Code.SubCode.Name);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #6
0
        public static void ReplacementMessageUsed()
        {
            string replacementEchoString = "bbbbb";
            var    inspector             = new MessageReplacingDispatchMessageInspector(replacementEchoString);
            var    behavior = new TestServiceBehavior {
                DispatchMessageInspector = inspector
            };
            var service = new DispatcherTestService();

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DispatcherTestService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton <IServiceBehavior>(behavior);
                services.AddSingleton(service);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();
            string         echo    = channel.Echo("hello");

            Assert.Equal(replacementEchoString, service.ReceivedEcho);
            Assert.Equal(replacementEchoString, echo);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #7
0
        public void IsSingletonShouldBeFalseWhenInstanceContextModeIsPerSession()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <PerSessionSimpleService, ISimpleService>();
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            channel.Echo(input);

            factory.Close();
        }
        public static void InstanceContextMode_PerCall_NoInjection()
        {
            PerCallInstanceContextSimpleService.ClearCounts();
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <PerCallInstanceContextSimpleService, ISimpleService>();
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            // Instance shouldn't be created as part of service startup as type isn't available in DI
            Assert.Equal(0, PerCallInstanceContextSimpleService.CreationCount);
            Assert.Equal(0, PerCallInstanceContextSimpleService.DisposalCount);

            string echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            PerCallInstanceContextSimpleService.WaitForDisposalCount(2, TimeSpan.FromSeconds(30));
            Assert.Equal(2, PerCallInstanceContextSimpleService.CreationCount);
            Assert.Equal(2, PerCallInstanceContextSimpleService.DisposalCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #9
0
        public void IsSingletonShouldBeTrueWhenInstanceContextModeIsSingleAndServiceIsRegisteredInDI()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleSimpleService, ISimpleService>(
                (services) =>
            {
                services.AddTransient <SingleSimpleService>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            channel.Echo(input);

            factory.Close();
        }
        public static void InjectedTransientInstanceWithServiceBehaviorPerCall_Succeeds()
        {
            DisposableSimpleService.InstantiationCount = 0;
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DisposableSimpleServiceWithServiceBehaviorPerCall, ISimpleService>(
                (services) =>
            {
                services.AddTransient <DisposableSimpleServiceWithServiceBehaviorPerCall>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
            Assert.Equal(1, DisposableSimpleService.InstantiationCount);
        }
예제 #11
0
        public static void OperationInvokerCalled()
        {
            TestDispatchOperationInvoker.ClearCounts();
            var behavior = new TestServiceBehavior {
                OperationInvokerFactory = TestDispatchOperationInvoker.Create
            };

            System.ServiceModel.ChannelFactory <ISimpleService> factory = ExtensibilityHelper.CreateChannelFactory <SimpleService, ISimpleService>(behavior);
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            Assert.Equal("hello", echo);
            Assert.Equal(1, TestDispatchOperationInvoker.InvokeCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #12
0
        public void InstanceProviderCalledTest()
        {
            var instanceProvider = new TestInstanceProvider();
            var behavior         = new TestServiceBehavior {
                InstanceProvider = instanceProvider
            };

            System.ServiceModel.ChannelFactory <ISimpleService> factory = ExtensibilityHelper.CreateChannelFactory <SimpleService, ISimpleService>(behavior);
            factory.Open();
            ISimpleService channel = factory.CreateChannel();
            string         echo    = channel.Echo("hello");

            Assert.Equal("hello", echo);
            instanceProvider.WaitForReleaseAsync(TimeSpan.FromSeconds(10)).Wait();
            Assert.Equal(1, instanceProvider.GetInstanceCallCount);
            Assert.Equal(1, instanceProvider.ReleaseInstanceCallCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
예제 #13
0
        public static void MessageInspectorCalled()
        {
            var inspector = new TestDispatchMessageInspector();
            var behavior  = new TestServiceBehavior {
                DispatchMessageInspector = inspector
            };

            System.ServiceModel.ChannelFactory <ISimpleService> factory = ExtensibilityHelper.CreateChannelFactory <SimpleService, ISimpleService>(behavior);
            factory.Open();
            ISimpleService channel = factory.CreateChannel();
            string         echo    = channel.Echo("hello");

            Assert.Equal("hello", echo);
            Assert.True(inspector.AfterReceiveCalled);
            Assert.True(inspector.BeforeSendCalled);
            Assert.True(inspector.CorrelationStateMatch);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InjectedSingletonInstanceWithServiceBehaviorSingle_NotDisposed()
        {
            DisposableSimpleService.InstantiationCount = 0;
            var serviceInstance = new DisposableSimpleServiceWithServiceBehaviorSingle();

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DisposableSimpleServiceWithServiceBehaviorSingle, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(serviceInstance);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.False(serviceInstance.IsDisposed, "Service instance shouldn't be disposed");
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
            Assert.Equal(1, DisposableSimpleService.InstantiationCount);
        }
        public static void InjectedTransientInstanceInjectedServiceBehaviorPerCall_Succeeds()
        {
            DisposableSimpleService.InstantiationCount = 0;
            var serviceBehaviorAttr = new CoreWCF.ServiceBehaviorAttribute();

            serviceBehaviorAttr.InstanceContextMode = CoreWCF.InstanceContextMode.PerCall;
            serviceBehaviorAttr.ConcurrencyMode     = CoreWCF.ConcurrencyMode.Multiple;
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DisposableSimpleService, ISimpleService>(
                (services) =>
            {
                services.AddTransient <DisposableSimpleService>();
                services.AddSingleton <IServiceBehavior>(serviceBehaviorAttr);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
            Assert.Equal(1, DisposableSimpleService.InstantiationCount);
        }
예제 #16
0
        public static void EndpointBehaviorUsed()
        {
            var endpointBehavior = new TestEndpointBehavior();

            System.ServiceModel.ChannelFactory <ISimpleService> factory = ExtensibilityHelper.CreateChannelFactory <SimpleService, ISimpleService>((CoreWCF.ServiceHostBase serviceHostBase) =>
            {
                foreach (var endpoint in serviceHostBase.Description.Endpoints)
                {
                    endpoint.EndpointBehaviors.Add(endpointBehavior);
                }
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();
            string         echo    = channel.Echo("hello");

            Assert.Equal("hello", echo);
            Assert.True(endpointBehavior.AddBindingParametersCalled);
            Assert.False(endpointBehavior.ApplyClientBehaviorCalled);
            Assert.True(endpointBehavior.ApplyDispatchBehaviorCalled);
            Assert.True(endpointBehavior.ValidateCalled);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }