How to access Ihostingenvironment outside of the Controller in ASP.NET Core?

Asked

Viewed 215 times

1

I’m updating an ASP.NET Framework API to ASP.NET Core and I’m stuck in a problem.

No. NET Framework used the following:

public GmailConnection(){
    serverCredentialPath = HostingEnvironment.MapPath("~/Credentials/Google/Gmail/"); //Variavel da classe sendo atribuida pelo mapeamento
    serverClientSecret = HostingEnvironment.MapPath("~/APIS/Google/Gmail/client_secret.json"); //Variavel da classe sendo atribuida pelo mapeamento
}

But in ASP.NET Core I can only access to IHostingEnvironment through the Controller:

private IHostingEnvironment _env;
public MarketingController(IHostingEnvironment env)
{
    _env = env;
    string path = env.WebRootPath;
}

How do I gain access to IHostingEnvironment through a class other than Controller?

  • would not be the case to assign this value there is an abstract class and static properties?

  • Yes, but how to receive the IHostingEnvironment in that statistical class?

  • I’ve tried so many ways...

  • Where will this class be? It is not clear to me where you want to use the IHostingEnvironment . One option may be to inject via constructor.

  • Barbetta, then, in the old version of my program, was inside the constructor of a class called GmailConnection, look at the issue. I need to receive these values inside the constructor or through a static class, the problem is that I cannot inject it via constructor...

1 answer

2


  1. You create a type variable IHostingEnvironment in class Startup.cs.
  2. Then assign the value received in the class constructor.

    private readonly IHostingEnvironment enviroment;
    
    public Startup(IHostingEnvironment env)
    {
        enviroment = env;
    
        // ...
    
    }      
    
  3. In the method ConfigureServices you register a service (used Singleton) passing the reference of IHostingEnvironment which was received in the class builder.

    public void Configureservices(Iservicecollection services) {

        services.AddMvc();
    
        services.AddSingleton(enviroment);
    }
    
  4. In the builder of Controller you continue to receive by dependency injection the instance of IHostingEnviroment.

        public HomeController(IHostingEnvironment env)
    {
        var gmailConnection = new GmailConnection(env);
    }
    
  5. In class (in case I created the class in a project ClassLibrary) you keep getting the instance of IHostingEnviroment by the builder.

    using Microsoft.AspNetCore.Hosting;

    namespace Infraestrutura
    {
        public class GmailConnection
        {
            private readonly string serverCredentialPath;
            private readonly string serverClientSecret;            
            private readonly IHostingEnvironment env;
    
        public GmailConnection(IHostingEnvironment env)
        {          
            this.env = env;
            string path = env.WebRootPath;             
        }
    }
    

Reference Injection of dependency in the class Statup.Cs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.