public static IHostBuilder DefineStatefulService(
            this IHostBuilder @this,
            Action <IStatefulServiceHostBuilder> externalConfigAction)
        {
            if (@this is null)
            {
                throw new ArgumentNullException(nameof(@this));
            }

            if (externalConfigAction is null)
            {
                throw new ArgumentNullException(nameof(externalConfigAction));
            }

            @this.InjectReliableService <IStatefulServiceHostBuilder>(
                internalConfigAction =>
            {
                var builder = new StatefulServiceHostBuilder();

                externalConfigAction(builder);
                internalConfigAction(builder);

                builder.ConfigureObject(
                    configurator =>
                {
                    switch (HostingEnvironment.GetRuntime())
                    {
                    case HostingRuntime.Default:
                        configurator.ConfigureDependencies(
                            dependencies =>
                        {
                            dependencies.AddSingleton <IServiceHostRuntime, ServiceHostRuntime>();
                        });
                        break;

                    case HostingRuntime.Local:
                        throw new NotSupportedException("Stateful Services can't be executed using Local Runtime");

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                });

                return(builder.Build());
            });

            return(@this);
        }
        public static IHostBuilder DefineStatelessService(
            this IHostBuilder @this,
            Action <IStatelessServiceHostBuilder> externalConfigAction)
        {
            if (@this is null)
            {
                throw new ArgumentNullException(nameof(@this));
            }

            if (externalConfigAction is null)
            {
                throw new ArgumentNullException(nameof(externalConfigAction));
            }

            @this.InjectReliableService <IStatelessServiceHostBuilder>(
                internalConfigAction =>
            {
                var builder = new StatelessServiceHostBuilder();

                externalConfigAction(builder);
                internalConfigAction(builder);

                builder.ConfigureObject(
                    configurator =>
                {
                    switch (HostingEnvironment.GetRuntime())
                    {
                    case HostingRuntime.Default:
                        configurator.ConfigureDependencies(
                            dependencies =>
                        {
                            dependencies.AddSingleton <IServiceHostRuntime, ServiceHostRuntime>();
                        });
                        break;

                    case HostingRuntime.Local:
                        configurator.ConfigureDependencies(
                            dependencies =>
                        {
                            dependencies.AddLogging(logging => logging.AddConsole());

                            dependencies.AddSingleton(
                                p => ActivatorUtilities.CreateInstance <ServiceActivationContextProvider>(p).GetActivationContext());
                            dependencies.AddSingleton(
                                p => ActivatorUtilities.CreateInstance <NodeContextProvider>(p).GetNodeContext());
                            dependencies.AddSingleton(
                                p => ActivatorUtilities.CreateInstance <ServicePackageProvider>(p).GetPackage());
                            dependencies.AddSingleton(
                                p => ActivatorUtilities.CreateInstance <ServiceManifestProvider>(p).GetManifest());
                            dependencies.AddSingleton(
                                p => ActivatorUtilities.CreateInstance <CodePackageActivationContextProvider>(p).GetActivationContext());

                            dependencies.AddTransient <IServiceManifestReader, ServiceManifestReader>();
                            dependencies.AddTransient <ICodePackageActivationContextReader, CodePackageActivationContextReader>();

                            dependencies.AddSingleton <IServiceHostRuntime, LocalRuntime>();
                        });

                        configurator.UseRemotingListenerReplicaTemplate(
                            () => new LocalRuntimeStatelessRemotingListenerReplicaTemplate());
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                });

                return(builder.Build());
            });

            return(@this);
        }