internal static UriPrefixTable <HandshakeDelegate> BuildAddressTable(IServiceProvider services)
        {
            ILogger <NetMessageFramingConnectionHandler> logger = services.GetRequiredService <ILogger <NetMessageFramingConnectionHandler> >();
            IServiceBuilder    serviceBuilder    = services.GetRequiredService <IServiceBuilder>();
            IDispatcherBuilder dispatcherBuilder = services.GetRequiredService <IDispatcherBuilder>();
            var addressTable = new UriPrefixTable <HandshakeDelegate>();

            foreach (Type serviceType in serviceBuilder.Services)
            {
                List <IServiceDispatcher> dispatchers = dispatcherBuilder.BuildDispatchers(serviceType);
                foreach (IServiceDispatcher dispatcher in dispatchers)
                {
                    if (dispatcher.BaseAddress == null)
                    {
                        // TODO: Should we throw? Ignore?
                        continue;
                    }

                    // TODO: Limit to specifically TcpTransportBindingElement if net.tcp etc
                    BindingElementCollection be = dispatcher.Binding.CreateBindingElements();
                    ConnectionOrientedTransportBindingElement cotbe = be.Find <ConnectionOrientedTransportBindingElement>();
                    if (cotbe == null)
                    {
                        // TODO: Should we throw? Ignore?
                        continue;
                    }

                    HandshakeDelegate handshake = BuildHandshakeDelegateForDispatcher(dispatcher);
                    logger.LogDebug($"Registering URI {dispatcher.BaseAddress} with NetMessageFramingConnectionHandler");
                    addressTable.RegisterUri(dispatcher.BaseAddress, cotbe.HostNameComparisonMode, handshake);
                }
            }

            return(addressTable);
        }
        private RequestDelegate BuildBranch(IApplicationBuilder app, IServiceBuilder serviceBuilder, IDispatcherBuilder dispatcherBuilder)
        {
            var branchApp       = app.New();
            var serverAddresses = app.ServerFeatures.Get <IServerAddressesFeature>();

            foreach (var address in serverAddresses.Addresses)
            {
                serviceBuilder.BaseAddresses.Add(new Uri(address));
            }

            foreach (var serviceType in serviceBuilder.Services)
            {
                var dispatchers = dispatcherBuilder.BuildDispatchers(serviceType);
                foreach (var dispatcher in dispatchers)
                {
                    if (dispatcher.BaseAddress == null)
                    {
                        // TODO: Should we throw? Ignore?
                        continue;
                    }

                    var scheme = dispatcher.BaseAddress?.Scheme;
                    if (!"http".Equals(scheme, StringComparison.OrdinalIgnoreCase) &&
                        !"https".Equals(scheme, StringComparison.OrdinalIgnoreCase))
                    {
                        continue; // Not an HTTP(S) dispatcher
                    }

                    bool matching = false;
                    foreach (var serverAddress in serverAddresses.Addresses)
                    {
                        // TODO: Might not be necessary to compare paths
                        var serverUri = new Uri(serverAddress);
                        var serverAddressNormalized = string.Join(':',
                                                                  serverUri.GetComponents(UriComponents.Port | UriComponents.Path, UriFormat.SafeUnescaped));
                        var dispatcherAddressNormalized = string.Join(':',
                                                                      dispatcher.BaseAddress.GetComponents(UriComponents.Port | UriComponents.Path, UriFormat.SafeUnescaped));
                        if (dispatcherAddressNormalized.StartsWith(serverAddressNormalized, StringComparison.OrdinalIgnoreCase))
                        {
                            matching = true;
                            break; // Dispatcher address is based on server listening address;
                        }
                    }

                    if (matching)
                    {
                        branchApp.Map(dispatcher.BaseAddress.AbsolutePath, wcfApp =>
                        {
                            var servicesScopeFactory = wcfApp.ApplicationServices.GetRequiredService <IServiceScopeFactory>();
                            wcfApp.Run(new RequestDelegateHandler(dispatcher, servicesScopeFactory).HandleRequest);
                        });
                    }
                }
            }

            branchApp.Use(_ => { return(context => _next(context)); });
            return(branchApp.Build());
        }
Exemplo n.º 3
0
        public static void BuildDispatcherWithConfiguration_XmlSerializer()
        {
            string serviceAddress = "http://localhost/dummy";

            var services = new ServiceCollection();

            services.AddLogging();
            services.AddServiceModelServices();

            IServer server = new MockServer();

            services.AddSingleton(server);
            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 <SimpleXmlSerializerService>();

            var binding = new CustomBinding("BindingName", "BindingNS");

            binding.Elements.Add(new MockTransportBindingElement());
            serviceBuilder.AddServiceEndpoint <SimpleXmlSerializerService, ISimpleXmlSerializerService>(binding, serviceAddress);
            serviceBuilder.OpenAsync().GetAwaiter().GetResult();

            IDispatcherBuilder dispatcherBuilder = serviceProvider.GetRequiredService <IDispatcherBuilder>();

            System.Collections.Generic.List <IServiceDispatcher> dispatchers = dispatcherBuilder.BuildDispatchers(typeof(SimpleXmlSerializerService));
            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 = XmlSerializerTestRequestContext.Create(serviceAddress);

            dispatcher.DispatchAsync(requestContext).Wait();
            Assert.True(requestContext.WaitForReply(TimeSpan.FromSeconds(5)), "Dispatcher didn't send reply");
            requestContext.ValidateReply();
        }
Exemplo n.º 4
0
        private RequestDelegate BuildBranch()
        {
            _logger.LogDebug("Building branch map");
            IApplicationBuilder branchApp = _app.New();

            foreach (Type serviceType in _serviceBuilder.Services)
            {
                System.Collections.Generic.List <IServiceDispatcher> dispatchers = _dispatcherBuilder.BuildDispatchers(serviceType);
                foreach (IServiceDispatcher dispatcher in dispatchers)
                {
                    if (dispatcher.BaseAddress == null)
                    {
                        // TODO: Should we throw? Ignore?
                        continue;
                    }

                    if (!(dispatcher.Binding is CustomBinding binding))
                    {
                        binding = new CustomBinding(dispatcher.Binding);
                    }
                    if (binding.Elements.Find <HttpTransportBindingElement>() == null)
                    {
                        _logger.LogDebug($"Binding for address {dispatcher.BaseAddress} is not an HTTP[S] binding ao skipping");
                        continue; // Not an HTTP(S) dispatcher
                    }

                    var parameters = new BindingParameterCollection
                    {
                        _app
                    };
                    Type supportedChannelType            = null;
                    IServiceDispatcher serviceDispatcher = null;
                    System.Collections.Generic.IList <Type> supportedChannels = dispatcher.SupportedChannelTypes;
                    for (int i = 0; i < supportedChannels.Count; i++)
                    {
                        Type channelType = supportedChannels[i];

                        if (channelType == typeof(IInputChannel))
                        {
                            if (binding.CanBuildServiceDispatcher <IInputChannel>(parameters))
                            {
                                serviceDispatcher    = binding.BuildServiceDispatcher <IInputChannel>(parameters, dispatcher);
                                supportedChannelType = typeof(IInputChannel);
                                break;
                            }
                        }
                        if (channelType == typeof(IReplyChannel))
                        {
                            if (binding.CanBuildServiceDispatcher <IReplyChannel>(parameters))
                            {
                                serviceDispatcher    = binding.BuildServiceDispatcher <IReplyChannel>(parameters, dispatcher);
                                supportedChannelType = typeof(IReplyChannel);
                            }
                        }
                        if (channelType == typeof(IDuplexChannel))
                        {
                            if (binding.CanBuildServiceDispatcher <IDuplexChannel>(parameters))
                            {
                                serviceDispatcher    = binding.BuildServiceDispatcher <IDuplexChannel>(parameters, dispatcher);
                                supportedChannelType = typeof(IDuplexChannel);
                            }
                        }
                        if (channelType == typeof(IInputSessionChannel))
                        {
                            if (binding.CanBuildServiceDispatcher <IInputSessionChannel>(parameters))
                            {
                                serviceDispatcher    = binding.BuildServiceDispatcher <IInputSessionChannel>(parameters, dispatcher);
                                supportedChannelType = typeof(IInputSessionChannel);
                            }
                        }
                        if (channelType == typeof(IReplySessionChannel))
                        {
                            if (binding.CanBuildServiceDispatcher <IReplySessionChannel>(parameters))
                            {
                                serviceDispatcher    = binding.BuildServiceDispatcher <IReplySessionChannel>(parameters, dispatcher);
                                supportedChannelType = typeof(IReplySessionChannel);
                            }
                        }
                        if (channelType == typeof(IDuplexSessionChannel))
                        {
                            if (binding.CanBuildServiceDispatcher <IDuplexSessionChannel>(parameters))
                            {
                                serviceDispatcher    = binding.BuildServiceDispatcher <IDuplexSessionChannel>(parameters, dispatcher);
                                supportedChannelType = typeof(IDuplexSessionChannel);
                            }
                        }
                    }

                    _logger.LogInformation($"Mapping CoreWCF branch app for path {dispatcher.BaseAddress.AbsolutePath}");
                    branchApp.Map(dispatcher.BaseAddress.AbsolutePath, wcfApp =>
                    {
                        IServiceScopeFactory servicesScopeFactory = wcfApp.ApplicationServices.GetRequiredService <IServiceScopeFactory>();
                        var requestHandler = new RequestDelegateHandler(serviceDispatcher, servicesScopeFactory);
                        if (requestHandler.WebSocketOptions != null)
                        {
                            wcfApp.UseWebSockets(requestHandler.WebSocketOptions);
                        }
                        wcfApp.Run(requestHandler.HandleRequest);
                    });
                }
            }

            branchApp.Use(_ => { return(reqContext => _next(reqContext)); });
            return(branchApp.Build());
        }