示例#1
0
        protected Func <TServiceInformation, IServiceEventSource> CreateEventSourceFunc(
            TParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var build = new Func <TServiceInformation, IServiceEventSource>(
                serviceInformation =>
            {
                var dependenciesCollection = parameters.DependenciesFunc();
                if (dependenciesCollection == null)
                {
                    throw new FactoryProducesNullInstanceException <IServiceCollection>();
                }

                dependenciesCollection.Add(serviceInformation.GetContext());
                dependenciesCollection.Add(serviceInformation.GetPartition());

                // Possible point of proxination
                parameters.DependenciesConfigAction?.Invoke(dependenciesCollection);

                // Adding support for open-generics
                var provider = new ProxynatorAwareServiceProvider(dependenciesCollection.BuildServiceProvider());

                return(parameters.ImplementationFunc(provider));
            });

            return(build);
        }
        protected override Func <ServiceContext, ICommunicationListener> CreateCommunicationListenerFunc(
            TService service,
            TParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (parameters.GenericCommunicationListenerFunc == null)
            {
                throw new InvalidOperationException(
                          $"No {nameof(parameters.GenericCommunicationListenerFunc)} was configured");
            }

            var listenerInformation = new ServiceHostGenericListenerInformation(parameters.EndpointName);

            return(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 ServiceHostGenericListenerLoggerProvider(
                            listenerInformation,
                            serviceContext,
                            serviceEventSource,
                            loggerOptions));
                });

                // Possible point of proxination
                parameters.DependenciesConfigAction?.Invoke(dependenciesCollection);

                // Adding open-generic proxies
                IServiceProvider provider = new ProxynatorAwareServiceProvider(dependenciesCollection.BuildServiceProvider());

                // Create instance of ICommunicationListener
                return parameters.GenericCommunicationListenerFunc(context, parameters.EndpointName, provider);
            });
        }
        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));
        }
示例#4
0
        protected Func <Func <Delegate, IServiceProvider, TDelegateInvoker>, TDelegateInvoker> CreateDelegateInvokerFunc(
            TService service,
            TParameters parameters)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var build = new Func <Func <Delegate, IServiceProvider, TDelegateInvoker>, TDelegateInvoker>(
                factory =>
            {
                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);

                var loggerOptions = parameters.LoggerOptionsFunc();
                if (loggerOptions == null)
                {
                    throw new FactoryProducesNullInstanceException <IConfigurableObjectLoggerOptions>();
                }

                dependenciesCollection.AddLogging(
                    builder =>
                {
                    builder.AddProvider(
                        new ServiceHostDelegateLoggerProvider(
                            serviceContext,
                            serviceEventSource,
                            loggerOptions));
                });

                // Possible point of proxination
                parameters.DependenciesConfigAction?.Invoke(dependenciesCollection);

                // Adding support for open-generics
                var provider = new ProxynatorAwareServiceProvider(dependenciesCollection.BuildServiceProvider());

                var invoker = factory(parameters.Delegate, provider);
                if (invoker == null)
                {
                    throw new FactoryProducesNullInstanceException <TDelegateInvoker>();
                }

                return(invoker);
            });

            return(build);
        }