public static void InstanceContextMode_Single() { SingleInstanceContextSimpleService.ClearCounts(); var serviceInstance = new SingleInstanceContextSimpleService(); var factory = ExtensibilityHelper.CreateChannelFactory <SingleInstanceContextSimpleService, ISimpleService>( (services) => { services.AddSingleton(serviceInstance); }); factory.Open(); var channel = factory.CreateChannel(); Assert.Equal(1, SingleInstanceContextSimpleService.AddBindingParametersCallCount); Assert.Equal(1, SingleInstanceContextSimpleService.ApplyDispatchBehaviorCount); Assert.Equal(1, SingleInstanceContextSimpleService.ValidateCallCount); var 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); }
public static void InstanceContextMode_PerCall() { PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts(); var factory = ExtensibilityHelper.CreateChannelFactory <PerCallInstanceContextSimpleServiceAndBehavior, ISimpleService>( (services) => { services.AddTransient <PerCallInstanceContextSimpleServiceAndBehavior>(); }); factory.Open(); var 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(); var echo = channel.Echo("hello"); echo = channel.Echo("hello"); 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_NoInjection() { PerCallInstanceContextSimpleService.ClearCounts(); var factory = ExtensibilityHelper.CreateChannelFactory <PerCallInstanceContextSimpleService, ISimpleService>(); factory.Open(); var 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); var echo = channel.Echo("hello"); echo = channel.Echo("hello"); 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); }