コード例 #1
0
ファイル: Startup.cs プロジェクト: kimkwangil2/BlazorDemoApp
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();
            services.AddRazorPages();
            services.AddServerSideBlazor().AddHubOptions(o =>
            {
                o.MaximumReceiveMessageSize = 10 * 1024 * 1024;
            });

            services.AddSingleton <WeatherForecastService>();

            services.AddTransient <ISqlDataAccess, SqlDataAccess>();
            services.AddTransient <IPeopleData, PeopleData>();

            services.AddScoped <IHttpService, HttpService>();
            services.AddScoped <IAccountService, AccountService>();
            services.AddScoped <ILocalStorageService, LocalStorageService>();

            services.AddScoped <ThemeState>();
            services.AddScoped <ExampleService>();
            services.AddScoped <NorthwindContext>();

            services.AddScoped <DialogService>();
            services.AddScoped <NotificationService>();
            services.AddScoped <TooltipService>();
            services.AddScoped <ContextMenuService>();
            services.AddScoped <NorthwindService>();
            services.AddScoped <NorthwindODataService>();

            services.AddScoped(x =>
            {
                var apiUrl = new Uri(Configuration.GetValue <string>("apiUrl"));
                // use fake backend if "fakeBackend" is "true" in appsettings.json
                if (Configuration.GetValue <string>("fakeBackend") == "true")
                {
                    var fakeBackendHandler = new FakeBackendHandler(x.GetService <ILocalStorageService>());
                    return(new HttpClient(fakeBackendHandler)
                    {
                        BaseAddress = apiUrl
                    });
                }
                return(new HttpClient()
                {
                    BaseAddress = apiUrl
                });
            });

            services.Configure <Microsoft.AspNetCore.Http.Features.FormOptions>(options =>
            {
                options.MultipartBodyLengthLimit = long.MaxValue;
            });
            services.AddLocalization();
        }
コード例 #2
0
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);

            builder.RootComponents.Add <App>("#app");

            //builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
            builder.Services
            .AddScoped <IProductHttpRepo, ProductHttpRepo>()
            .AddScoped <IAccountService, AccountService>()
            .AddScoped <IAlertService, AlertService>()
            .AddScoped <IHttpService, HttpService>()
            .AddScoped <ILocalStorageService, LocalStorageService>();

            // configure http client
            builder.Services.AddScoped(x =>
            {
                var apiUrl = new Uri(builder.HostEnvironment.BaseAddress);

                // use fake backend if "fakeBackend" is "true" in appsettings.json
                if (builder.Configuration["fakeBackend"] == "true")
                {
                    var fakeBackendHandler = new FakeBackendHandler(x.GetService <ILocalStorageService>());
                    return(new HttpClient(fakeBackendHandler)
                    {
                        BaseAddress = apiUrl
                    });
                }

                return(new HttpClient()
                {
                    BaseAddress = apiUrl
                });
            });

            var host = builder.Build();

            var accountService = host.Services.GetRequiredService <IAccountService>();
            await accountService.Initialize();

            await host.RunAsync();

            //await builder.Build().RunAsync();
        }