Use httpclientfactory in project with both .net core 3.1 and .net framework 4.7.2

753 views Asked by At

Im upgrading an old CMS from WebForms .Fet Framework to .net core. Im starting with the frontend where i have implemented httpclientfactory.

Until i upgrade the backend i would like to use the same data layer on both the .net core and the .net framework solution, but im pretty stucked how to implement it in my WebForms solution

In my API helper class which can be accessed from both webforms and .net core i have implemented the following:

private IHttpClientFactory _httpClientFactory;
public WebSite(IHttpClientFactory httpClientFactory)
{
    _httpClientFactory = httpClientFactory;
}

and the function to get data is:

public async Task<WebSite> GetSingle(int id)
{
   var client = _httpClientFactory.CreateClient();     // and so on ///      
}

On the .net core solution i initialize the HttpClientFactory like:

private readonly IHttpClientFactory _clientFactory;
private readonly WebSite _webSite;

In the net core solution og initialize the IHttpClientFactory

public IndexModel(IHttpClientFactory clientFactory)
{
   _clientFactory = clientFactory;
   _webSite = new WebSite(_clientFactory);
}

And the i get the data like :

WebSite currentWebsite = _webSite.GetSingle(1);

I have done the following in my WebForms app:

Installed

Autofac.Extensions.DependencyInjection
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Hosting
Microsoft.Extensions.Http

Added the following to global.asax

public static IServiceProvider ServiceProvider;

protected void Application_Start(object sender, EventArgs e)
{
    var hostBuilder = new HostBuilder();
    hostBuilder.ConfigureServices(ConfigureServices);
    var host = hostBuilder.Build();
    ServiceProvider = host.Services;
}

void ConfigureServices(IServiceCollection services)
{
    services.AddHttpClient(); //extension method that add IHttpClientFactories
    var providerFactory = new AutofacServiceProviderFactory();
    providerFactory.CreateBuilder(services);
}

The question is, how do I inject a httpclientfactoryclient into my WebForms App so I can use _webSite.GetSingle(1) as in the .net core solution

1

There are 1 answers

0
Keld Jakobsen On

Unity solved the problem.

Registered a container in Global.asax

var container = Microsoft.AspNet.WebFormsDependencyInjection.Unity.UnityAdapter.AddUnity();
container.RegisterType<IHttpClientFactory, CMSHttpClientFactory>();

where CMSHttpClientFactory is a very simpel implementation of IHttpClientFactory

Created a base System.Web.UI.Page (and UserControl and MasterPage) where I get the clientfactory again

public IHttpClientFactory _httpClientFactory;

public BasePage()
{
   var container = Microsoft.AspNet.WebFormsDependencyInjection.Unity.UnityAdapter.GetContainer();
   _httpClientFactory = container.Resolve<Api.CMSHttpClientFactory>();
}

Which i then again pass to my Helperclass. Now i can use the same API call on both .net core and webfors 4.7.2