Пример #1
0
    public static void BuildDispatcherWithConfiguration_Singleton_Not_WellKnown()
    {
        string serviceAddress = "http://localhost/dummy";
        var    services       = new ServiceCollection();

        services.AddServiceModelServices();
        var serverAddressesFeature = new ServerAddressesFeature();

        serverAddressesFeature.Addresses.Add(serviceAddress);
        IServer server = new MockServer();

        server.Features.Set <IServerAddressesFeature>(serverAddressesFeature);
        services.AddSingleton(server);
        var serviceProvider = services.BuildServiceProvider();
        var serviceBuilder  = serviceProvider.GetRequiredService <IServiceBuilder>();

        serviceBuilder.AddService <SimpleSingletonService>();
        var binding = new CustomBinding("BindingName", "BindingNS");

        binding.Elements.Add(new MockTransportBindingElement());
        serviceBuilder.AddServiceEndpoint <SimpleSingletonService, ISimpleService>(binding, serviceAddress);
        var dispatcherBuilder = serviceProvider.GetRequiredService <IDispatcherBuilder>();
        var dispatchers       = dispatcherBuilder.BuildDispatchers(typeof(SimpleSingletonService));

        Assert.Single(dispatchers);
        var dispatcher = dispatchers[0];

        Assert.Equal("foo", dispatcher.Binding.Scheme);
        Assert.Equal(serviceAddress, dispatcher.BaseAddress.ToString());
        var      requestContext = TestRequestContext.Create(serviceAddress);
        IChannel mockChannel    = new MockReplyChannel(serviceProvider);

        dispatcher.DispatchAsync(requestContext, mockChannel, CancellationToken.None).Wait();
        requestContext.ValidateReply();
    }
Пример #2
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);
        }
Пример #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);
        }
Пример #4
0
        public static void InstanceContextMode_PerSession_NoInjection()
        {
            PerSessionInstanceContextSimpleService.ClearCounts();
            var      services          = new ServiceCollection();
            string   serviceAddress    = "http://localhost/dummy";
            var      serviceDispatcher = TestHelper.BuildDispatcher <PerSessionInstanceContextSimpleService>(services, serviceAddress);
            IChannel mockChannel       = new MockReplySessionChannel(services.BuildServiceProvider());
            var      dispatcher        = serviceDispatcher.CreateServiceChannelDispatcher(mockChannel);

            // 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);

            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, PerSessionInstanceContextSimpleService.CreationCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleService.DisposalCount);
        }
Пример #5
0
        public static void InstanceContextMode_Single()
        {
            SingleInstanceContextSimpleService.ClearCounts();
            var services        = new ServiceCollection();
            var serviceInstance = new SingleInstanceContextSimpleService();

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

            Assert.Equal(1, SingleInstanceContextSimpleService.AddBindingParametersCallCount);
            Assert.Equal(1, SingleInstanceContextSimpleService.ApplyDispatchBehaviorCount);
            Assert.Equal(1, SingleInstanceContextSimpleService.ValidateCallCount);

            var requestContext = TestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            requestContext = TestRequestContext.Create(serviceAddress);
            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
            Assert.Equal(1, SingleInstanceContextSimpleService.CreationCount);
            Assert.Equal(0, SingleInstanceContextSimpleService.DisposalCount);
            Assert.Equal(2, serviceInstance.CallCount);
        }
Пример #6
0
        public static void BuildDispatcherAndCallService(ServiceCollection services)
        {
            string   serviceAddress    = "http://localhost/dummy";
            var      serviceDispatcher = BuildDispatcher <SimpleService>(services, serviceAddress);
            IChannel mockChannel       = new MockReplyChannel(services.BuildServiceProvider());
            var      dispatcher        = serviceDispatcher.CreateServiceChannelDispatcher(mockChannel);
            var      requestContext    = TestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext, CancellationToken.None).Wait();
        }
Пример #7
0
        public static void BuildDispatcherWithConfiguration_Singleton_WellKnown()
        {
            string serviceAddress = "http://localhost/dummy";
            var    services       = new ServiceCollection();

            services.AddLogging();
            services.AddServiceModelServices();
            IServer server = new MockServer();

            services.AddSingleton(server);
            services.AddSingleton(new SimpleSingletonService());
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));
            services.RegisterApplicationLifetime();
            ServiceProvider serviceProvider = services.BuildServiceProvider();
            IServiceBuilder serviceBuilder  = serviceProvider.GetRequiredService <IServiceBuilder>();

            serviceBuilder.BaseAddresses.Add(new Uri(serviceAddress));
            serviceBuilder.AddService <SimpleSingletonService>();
            var binding = new CustomBinding("BindingName", "BindingNS");

            binding.Elements.Add(new MockTransportBindingElement());
            serviceBuilder.AddServiceEndpoint <SimpleSingletonService, ISimpleService>(binding, serviceAddress);
            serviceBuilder.OpenAsync().GetAwaiter().GetResult();
            IDispatcherBuilder dispatcherBuilder = serviceProvider.GetRequiredService <IDispatcherBuilder>();

            System.Collections.Generic.List <IServiceDispatcher> dispatchers = dispatcherBuilder.BuildDispatchers(typeof(SimpleSingletonService));
            Assert.Single(dispatchers);
            IServiceDispatcher serviceDispatcher = dispatchers[0];

            Assert.Equal("foo", serviceDispatcher.Binding.Scheme);
            Assert.Equal(serviceAddress, serviceDispatcher.BaseAddress.ToString());
            IChannel mockChannel = new MockReplyChannel(serviceProvider);
            IServiceChannelDispatcher dispatcher = serviceDispatcher.CreateServiceChannelDispatcherAsync(mockChannel).Result;
            var requestContext = TestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext).Wait();
            Assert.True(requestContext.WaitForReply(TimeSpan.FromSeconds(5)), "Dispatcher didn't send reply");
            requestContext.ValidateReply();
        }
Пример #8
0
        public static void BuildDispatcherWithConfiguration()
        {
            string serviceAddress = "http://localhost/dummy";
            var    services       = new ServiceCollection();

            services.AddServiceModelServices();
            var serverAddressesFeature = new ServerAddressesFeature();

            serverAddressesFeature.Addresses.Add(serviceAddress);
            IServer server = new MockServer();

            server.Features.Set <IServerAddressesFeature>(serverAddressesFeature);
            services.AddSingleton(server);
            var serviceProvider = services.BuildServiceProvider();
            var serviceBuilder  = serviceProvider.GetRequiredService <IServiceBuilder>();

            serviceBuilder.AddService <SimpleService>();
            var binding = new CustomBinding("BindingName", "BindingNS");

            binding.Elements.Add(new MockTransportBindingElement());
            serviceBuilder.AddServiceEndpoint <SimpleService, ISimpleService>(binding, serviceAddress);
            var dispatcherBuilder = serviceProvider.GetRequiredService <IDispatcherBuilder>();
            var dispatchers       = dispatcherBuilder.BuildDispatchers(typeof(SimpleService));

            Assert.Single(dispatchers);
            var serviceDispatcher = dispatchers[0];

            Assert.Equal("foo", serviceDispatcher.Binding.Scheme);
            Assert.Equal(serviceAddress, serviceDispatcher.BaseAddress.ToString());
            IChannel mockChannel    = new MockReplyChannel(serviceProvider);
            var      dispatcher     = serviceDispatcher.CreateServiceChannelDispatcherAsync(mockChannel).Result;
            var      requestContext = TestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext).Wait();
            Assert.True(requestContext.WaitForReply(TimeSpan.FromSeconds(5)), "Dispatcher didn't send reply");
            requestContext.ValidateReply();
        }