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);
        }
        public static void InstanceContextMode_PerSession_WithScopedCtorDependency()
        {
            PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.ClearCounts();
            System.ServiceModel.ChannelFactory <ISimpleSessionService> factory = DispatcherHelper.CreateChannelFactory <PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency, ISimpleSessionService>(
                (services) =>
            {
                services.AddScoped <IScopedCtorDependency, ScopedCtorDependency>();
                services.AddTransient <PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency>();
            });
            factory.Open();
            ISimpleSessionService 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, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.CreationCount);
            // Instance not disposed as it implements IServiceBehavior and is added to service behaviors
            Assert.Equal(0, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.DisposalCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.AddBindingParametersCallCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.ApplyDispatchBehaviorCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.ValidateCallCount);

            PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.ClearCounts();
            ScopedCtorDependency.ClearCounts();

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

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

            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.CreationCount);
            PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.WaitForDisposalCount(1, TimeSpan.FromSeconds(30));
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehaviorWithScopedCtorDependency.DisposalCount);

            ScopedCtorDependency.WaitForDisposalCount(1, TimeSpan.FromSeconds(30));
            Assert.Equal(1, ScopedCtorDependency.CreationCount);
            Assert.Equal(1, ScopedCtorDependency.DisposalCount);

            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InstanceContextMode_PerSession_NoInjection()
        {
            PerSessionInstanceContextSimpleService.ClearCounts();
            System.ServiceModel.ChannelFactory <ISimpleSessionService> factory = DispatcherHelper.CreateChannelFactory <PerSessionInstanceContextSimpleService, ISimpleSessionService>();
            factory.Open();
            ISimpleSessionService channel = factory.CreateChannel();

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

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

            echo = channel.Echo("hello");
            echo = channel.Echo("hello");
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.Equal(1, PerSessionInstanceContextSimpleService.CreationCount);
            PerSessionInstanceContextSimpleService.WaitForDisposalCount(1, TimeSpan.FromSeconds(30));
            Assert.Equal(1, PerSessionInstanceContextSimpleService.DisposalCount);
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }