예제 #1
0
        public static void InstanceContextMode_PerCall_WithBehavior_NoInjection()
        {
            PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var factory = DispatcherHelper.CreateChannelFactory <PerCallInstanceContextSimpleServiceAndBehavior, ISimpleService>();

            factory.Open();
            var channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            // Instance created as part of service startup as it implements IServiceBehavior
            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();
            var echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            PerCallInstanceContextSimpleServiceAndBehavior.WaitForDisposalCount(2, TimeSpan.FromSeconds(5));
            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_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);
        }
예제 #3
0
        public static void InstanceContextMode_PerCall()
        {
            PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var services = new ServiceCollection();

            services.AddTransient <PerCallInstanceContextSimpleServiceAndBehavior>();
            string   serviceAddress    = "http://localhost/dummy";
            var      serviceDispatcher = TestHelper.BuildDispatcher <PerCallInstanceContextSimpleServiceAndBehavior>(services, serviceAddress);
            IChannel mockChannel       = new MockReplyChannel(services.BuildServiceProvider());
            var      dispatcher        = serviceDispatcher.CreateServiceChannelDispatcher(mockChannel);

            // 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();
            var requestContext = TestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            requestContext = TestRequestContext.Create(serviceAddress);
            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount);
            Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount);
        }