示例#1
0
        public static void InstanceContextMode_PerSession()
        {
            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var factory = DispatcherHelper.CreateChannelFactory <PerSessionInstanceContextSimpleServiceAndBehavior, ISimpleSessionService>(
                (services) =>
            {
                services.AddTransient <PerSessionInstanceContextSimpleServiceAndBehavior>();
            });

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

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

            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            echo = channel.Echo("hello");
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.CreationCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.DisposalCount);
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InstanceContextMode_PerSession_WithBehavior_NoInjection()
        {
            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            System.ServiceModel.ChannelFactory <ISimpleSessionService> factory = DispatcherHelper.CreateChannelFactory <PerSessionInstanceContextSimpleServiceAndBehavior, ISimpleSessionService>();
            factory.Open();
            ISimpleSessionService channel = factory.CreateChannel();

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

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

            echo = channel.Echo("hello");
            echo = channel.Echo("hello");
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.CreationCount);
            PerSessionInstanceContextSimpleServiceAndBehavior.WaitForDisposalCount(1, TimeSpan.FromSeconds(30));
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.DisposalCount);
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#3
0
        public static void InstanceContextMode_PerSession_WithBehavior_NoInjection()
        {
            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var      services          = new ServiceCollection();
            string   serviceAddress    = "http://localhost/dummy";
            var      serviceDispatcher = TestHelper.BuildDispatcher <PerSessionInstanceContextSimpleServiceAndBehavior>(services, serviceAddress);
            IChannel mockChannel       = new MockReplySessionChannel(services.BuildServiceProvider());
            var      dispatcher        = serviceDispatcher.CreateServiceChannelDispatcher(mockChannel);

            // Instance created as part of service startup as it implements IServiceBehavior
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.CreationCount);
            // Instance not disposed as it implements IServiceBehavior and is added to service behaviors
            Assert.Equal(0, PerSessionInstanceContextSimpleServiceAndBehavior.DisposalCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.AddBindingParametersCallCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.ApplyDispatchBehaviorCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.ValidateCallCount);

            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var requestContext = TestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            requestContext = TestRequestContext.Create(serviceAddress);
            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            requestContext = TestRequestContext.Create(serviceAddress);
            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            // Close channel/session by sending null request context
            dispatcher.DispatchAsync(null, CancellationToken.None).Wait();
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.CreationCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.DisposalCount);
        }