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()); }
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>(); var requestHandler = new RequestDelegateHandler(dispatcher, servicesScopeFactory); if (requestHandler.WebSocketOptions != null) { wcfApp.UseWebSockets(requestHandler.WebSocketOptions); } wcfApp.Run(requestHandler.HandleRequest); }); } } } branchApp.Use(_ => { return(context => _next(context)); }); return(branchApp.Build()); }