İçeriğe geç

.NET Core 3.0 içerisindeki IHostingEnvironment ile IHostEnvironment – obsolete tipi

.Net 3.0 ile gelen güncellemelerden bir tanesi de HostingEnviroment sınıfında yapılan bir takım temel değişiklikler.

namespace Microsoft.AspNetCore.Hosting
{
    public interface IHostingEnvironment
    {
        string EnvironmentName { get; set; }
        string ApplicationName { get; set; }
        string WebRootPath { get; set; }
        IFileProvider WebRootFileProvider { get; set; }
        string ContentRootPath { get; set; }
        IFileProvider ContentRootFileProvider { get; set; }
    }
}

namespace Microsoft.Extensions.Hosting
{
    public interface IHostingEnvironment
    {
        string EnvironmentName { get; set; }
        string ApplicationName { get; set; }
        string ContentRootPath { get; set; }
        IFileProvider ContentRootFileProvider { get; set; }
    }
}

Temelde içerisinde yukarıdaki gibi parametreleri barındıran iki adet HostingEnvironment sınıfı mevcut. Bunlardan bir tanesi “Microsoft.AspNetCore.Hosting” diğeri ise “Microsoft.Extensions.Hosting” sınıfına ait namespace’ler.

Tabi bu sınıf adları .net core 3.0’a kadar geçerli idi. .Net Core 3.0 itibari ile (3.1 ve muhtemelen üzeri de dahil olacak) aşağıdaki şekilde değiştirildi.

namespace Microsoft.Extensions.Hosting
{
    public interface IHostEnvironment
    {
        string EnvironmentName { get; set; }
        string ApplicationName { get; set; }
        string ContentRootPath { get; set; }
        IFileProvider ContentRootFileProvider { get; set; }
    }
}

namespace Microsoft.AspNetCore.Hosting
{
    public interface IWebHostEnvironment : IHostEnvironment
    {
        string WebRootPath { get; set; }
        IFileProvider WebRootFileProvider { get; set; }
    }
}

Bu şekilde gördüğünüz gibi aslında iki sınıfta birbirinden bir miktar daha farklı veriler tutar hale geldi. Aslında bu şekilde daha da bir anlam kazandığını söylemek yalan olmayacaktır.

Yani kısacası, .net core 3.0 veya üzeri kodlama yapıyorsanız ve aşağıdaki şekilde bir tanımlamaya ihtiyaç duyuyor iseniz;

private readonly IHostingEnvironment _environment;

public HomeController(IHostingEnvironment environment)
{
    this._environment = environment;
}

IHostingEnvironment interface’inin altının çizildiğini ve ancak size “obsolete” (eski/modası geçmiş) şekilde bir attribute ile hizmet verebileceğini belirtir. Eğer bu şekilde attribute’ü eklerseniz de constructor içerisinde tanım yapmaktan muaf olacaksınız maalesef. Bu gibi bir durumda kodunuzu aşağıdaki şekilde değiştirmeniz ve using olarak ekleme yapmanız yeterli olacaktır.

using Microsoft.Extensions.Hosting;

private readonly IHostEnvironment _environment;

public HomeController(IHostEnvironment environment)
{
    this._environment = environment;
}

Günün sonunda, projemizdeki bu hatayı ortadan kaldırmış bulunmaktayız.

0 0 votes
Article Rating
Tarih:Çerez BilgilerHatalar ve Çözümler
Subscribe
Bildir
guest
0 Yorum
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x