dimanche 4 octobre 2020

Net core 3.1 IWebHostBuilder change port for client

So, I am trying to create a custom IWebHostBuilder, but I am hitting 404s since the port is not correct. Here is the code:

I tried calling: builder.UseSetting("applicationUrl", "http://localhost:35072/");

and builder.UseSetting("launchUrl", "http://localhost:35072/");

and also tried to inject the launchSettings.json file at the end.

What can I do here?

    class Program
    {
        static void Main(string[] args)
        {
            using (var host = new CustomWebApplicationFactory<Startup>())
            {
                var client = host.Server.CreateClient();
                var response = client.GetAsync("/api/hackernews/fetch?pageIndex=0&pageSize=50").Result;
                Console.WriteLine(response);
            }
        }

        private static async Task<HttpClient> InitAsync()
        {
            var hostBuilder = new HostBuilder()
                .UseContentRoot(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName)
                .ConfigureWebHost(webHost =>
                {
                    webHost.UseTestServer();
                    webHost.ConfigureServices((context, services )=>
                    {
                        var assembly = typeof(Startup).Assembly;

                        services.AddControllers()
                            .PartManager.ApplicationParts.Add(new AssemblyPart(assembly));
                        var startup = new Startup(context.Configuration);
                        startup.ConfigureServices(services);
                    });
                    
                    webHost.Configure(app =>
                    {
                        app.UseRouting();

                        app.UseCors("CorsPolicy");

                        app.UseAuthorization();

                        app.UseEndpoints(endpoints =>
                        {
                            endpoints.MapControllers();
                        });
                    });
                });

            // Build and start the IHost
            var host = await hostBuilder.StartAsync();

            return host.GetTestClient();
        }
    }

    public class CustomWebApplicationFactory<TStartup>
        : WebApplicationFactory<TStartup> where TStartup: class
    {
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            var root = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName;
            builder.UseContentRoot(root);
            builder.UseSetting("applicationUrl", "http://localhost:35072/");
            builder.UseSetting("launchUrl", "http://localhost:35072/");
            builder.ConfigureServices(services =>
            {
                services.AddCors(options =>
                {
                    options.AddPolicy("CorsPolicy",
                        builder => builder.AllowAnyOrigin()
                            .AllowAnyMethod()
                            .AllowAnyHeader());
                });
                services.AddMemoryCache();
                services.AddControllers().AddNewtonsoftJson(options =>
                    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                );

            });

            builder.Configure(app =>
            {
                
                app.UseRouting();

                app.UseCors("CorsPolicy");

                app.UseAuthorization();

                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });

                
            });

            builder.ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile(Path.Combine(root, "HackerNewsTask", "Properties", "launchSettings.json"));
            });
        }
    }

enter image description here

Aucun commentaire:

Enregistrer un commentaire