public static IGrpcHostBuilder CreateDefaultBuilder(string[] args)
        {
            var builder = new GrpcHostBuilder()
                          .UseContentRoot(Directory.GetCurrentDirectory())
                          .ConfigureAppConfiguration((context, config) =>
            {
                var env = context.HostingEnvironment;

                config.AddJsonFile("appsettings.json", true, true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);

                config.AddEnvironmentVariables();

                if (args != null)
                {
                    config.AddCommandLine(args);
                }
            })
                          .ConfigureLogging((context, logging) =>
            {
                logging.AddConfiguration(context.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
            })
                          .UseDefaultServiceProvider((context, options) =>
            {
                options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
            });

            return(builder);
        }
Example #2
0
 public void EnvDefaultsToProductionIfNoConfig()
 {
     using (var host = new GrpcHostBuilder().UseStartup <Fakes.Startup>().Build())
     {
         var env = host.Services.GetService <IHostingEnvironment>();
         Assert.Equal(EnvironmentName.Production, env.EnvironmentName);
     }
 }
Example #3
0
 public void WebHost_InvokesConfigureMethodsOnlyOnce()
 {
     using (var host = new GrpcHostBuilder().UseStartup <CountStartup>().Build())
     {
         host.Start();
         Assert.Equal(1, CountStartup.ConfigureServicesCount);
         Assert.Equal(1, CountStartup.ConfigureCount);
     }
 }
Example #4
0
 public void IsEnvironment_Extension_Is_Case_Insensitive()
 {
     using (var host = new GrpcHostBuilder().UseStartup <Fakes.Startup>().Build())
     {
         host.Start();
         var env = host.Services.GetService <IHostingEnvironment>();
         Assert.True(env.IsEnvironment(EnvironmentName.Production));
         Assert.True(env.IsEnvironment("producTion"));
     }
 }
Example #5
0
        public void EnvDefaultsToConfigValueIfSpecified()
        {
            var vals = new Dictionary <string, string>
            {
                { "Environment", EnvironmentName.Staging }
            };

            var builder = new ConfigurationBuilder().AddInMemoryCollection(vals);
            var config  = builder.Build();

            using (var host = new GrpcHostBuilder().UseConfiguration(config).UseStartup <Fakes.Startup>().Build())
            {
                var env = host.Services.GetService <IHostingEnvironment>();
                Assert.Equal(EnvironmentName.Staging, env.EnvironmentName);
            }
        }
Example #6
0
        private static async Task Main(string[] args)
        {
            await
            GrpcHostBuilder.BuildHost <Program>(args, (ctx, svcs) =>
            {
                svcs.AddSingleton <HttpClient>();
                svcs.AddTransient <ICustomerService, Services.CustomerService>();

                svcs.AddSingleton <CustomerServiceImpl>();
                svcs.AddSingleton <ProductServiceImpl>();

                svcs.AddSingleton <IMethodContext>(x =>
                                                   new MethodContext <GetCustomerByIdRequest, GetCustomerByIdResponse, CustomerServiceImpl>(
                                                       ActivatorUtilities.GetServiceOrCreateInstance <CustomerServiceImpl>(x),
                                                       ActivatorUtilities.GetServiceOrCreateInstance <MyInterceptor>(x)));

                svcs.AddSingleton <IMethodContext, MethodContext <DeleteCustomerByIdRequest, DeleteCustomerByIdResponse, CustomerServiceImpl> >();
                svcs.AddSingleton <IMethodContext, MethodContext <CustomerSearch, Customer, CustomerServiceImpl> >();
                svcs.AddSingleton <IMethodContext, MethodContext <GetProductsForCustomerRequest, GetProductsForCustomerResponse, ProductServiceImpl> >();
            })
            .RunAsync().ConfigureAwait(false);
        }