private static FabricTransportServiceRemotingListener DefaultRemotingCommunicationListenerFunc( ServiceContext serviceContext, ServiceHostRemotingCommunicationListenerComponentsFactory build) { var components = build(serviceContext); return(new FabricTransportServiceRemotingListener( serviceContext, components.MessageHandler, components.ListenerSettings, components.MessageSerializationProvider)); }
protected override Func <ServiceContext, ICommunicationListener> CreateCommunicationListenerFunc( TService service, TParameters parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (parameters.RemotingImplementationFunc == null) { throw new InvalidOperationException( $"No {nameof(parameters.RemotingImplementationFunc)} was configured"); } var listenerInformation = new ServiceHostRemotingListenerInformation(parameters.EndpointName); var build = new ServiceHostRemotingCommunicationListenerComponentsFactory( context => { var serviceContext = service.GetContext(); var servicePartition = service.GetPartition(); var serviceEventSource = service.GetEventSource(); var dependenciesCollection = parameters.DependenciesFunc(); if (dependenciesCollection == null) { throw new FactoryProducesNullInstanceException <IServiceCollection>(); } // We need register all level dependencies first in order to make // sure that no level dependencies will be ignore during proxination dependenciesCollection.Add(serviceContext); dependenciesCollection.Add(servicePartition); dependenciesCollection.Add(serviceEventSource); dependenciesCollection.Add(listenerInformation); var loggerOptions = parameters.LoggerOptionsFunc(); if (loggerOptions == null) { throw new FactoryProducesNullInstanceException <IConfigurableObjectLoggerOptions>(); } dependenciesCollection.AddLogging( builder => { builder.AddProvider( new ServiceHostRemotingListenerLoggerProvider( listenerInformation, serviceContext, serviceEventSource, loggerOptions)); }); // Possible point of proxination parameters.DependenciesConfigAction?.Invoke(dependenciesCollection); // Adding open-generic proxies IServiceProvider provider = new ProxynatorAwareServiceProvider(dependenciesCollection.BuildServiceProvider()); var implementation = parameters.RemotingImplementationFunc(provider); if (implementation == null) { throw new FactoryProducesNullInstanceException <IRemotingImplementation>(); } var implementationType = implementation.GetType(); var replacements = new Dictionary <Type, object> { [implementationType] = implementation, [typeof(IRemotingImplementation)] = implementation }; // Adding implementation as singleton provider = new ReplaceAwareServiceProvider(replacements, provider); var serializer = (IServiceRemotingMessageSerializationProvider)null; if (parameters.RemotingSerializationProviderFunc != null) { serializer = parameters.RemotingSerializationProviderFunc(provider); if (serializer == null) { throw new FactoryProducesNullInstanceException <IServiceRemotingMessageSerializationProvider>(); } } var settings = parameters.RemotingSettingsFunc(); if (settings == null) { throw new FactoryProducesNullInstanceException <FabricTransportRemotingListenerSettings>(); } settings.EndpointResourceName = parameters.EndpointName; var logger = (ILogger)provider.GetService(typeof(ILogger <>).MakeGenericType(implementation.GetType())); var handler = parameters.RemotingHandlerFunc(provider); return(new ServiceHostRemotingCommunicationListenerComponents( new ServiceHostRemotingListenerMessageHandler(handler, logger), serializer, settings)); }); return(context => parameters.RemotingCommunicationListenerFunc(context, build)); }