Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to correctly create middleware extensions?

$
0
0

I am refactoring my code and moving some classes to an external assembly. In my projects I use Refit and currently I set it up like this in Startup:

services.AddHttpClient();services.AddRefitClient<IMyServerService>(new RefitSettings(new NewtonsoftJsonContentSerializer()))    .AddHttpMessageHandler<MyServerHandler>()    .ConfigureHttpClient(c => c.BaseAddress = new Uri(configuration["App:MyServerUrl"]));

Now, in my projects I would like to have something like this:

services.AddServers(options =>{    options.MyServerUrl = configuration["App:MyServerUrl"];});

So I am trying to create the following extension:

public class ServerOptions{    public string MyServerUrl { get; set; }}public static IServiceCollection AddServers(this IServiceCollection services,     Action<ServerOptions> configureOptions = null){    if (configureOptions is null)        return services;    services.AddHttpClient();    services.AddRefitClient<IMyServerService>(new RefitSettings(new NewtonsoftJsonContentSerializer()))        .AddHttpMessageHandler<MyServerHandler>()        .ConfigureHttpClient(c => c.BaseAddress = new Uri(...));    return services;}

How do I correctly set up the configuration options so the the property is passed to the based address of the HttpClient?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>