繁体   English   中英

.NET Core 3.1 - HostingEnvironment 不包含 MapPath 的定义

[英].NET Core 3.1 - HostingEnvironment does not contain a definition for MapPath

我正在使用 .NET Core 3.1,我正尝试通过提供私钥连接到 Google 的索引 API。 我看过类似的东西: Google Indexing API Batch Request using .NET and https://hamidmosalla.com/2019/12/07/using-google-indexing-api-with-google-api-client-library-for 示例。网/

这是我的代码:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Requests;
using Google.Apis.Services;
using Google.Apis.Indexing.v3;
using Google.Apis.Indexing.v3.Data;
using Microsoft.Extensions.Hosting.Internal;

public static class MyClassDownloader {
    public static GoogleCredential GetGoogleCredential()
    {    
        string path = HostingEnvironment.MapPath("/PrivateKey/myprivatekey.json");
        GoogleCredential credential;
    
        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            credential = GoogleCredential.FromStream(stream).CreateScoped(new[] { "https://www.googleapis.com/auth/indexing" });
        }
    
        return credential;
    }
}

但是,我在有关MapPath的行中收到错误消息:

'HostingEnvironment' does not contain a definition for 'MapPath'

我曾尝试查看IHostingEnvironment ,之后我了解到IWebHostEnvironment已弃用它。 无论我是在开发中还是在生产中,如何为我的 JSON 文件获取正确的物理路径?

我将MyClassDownloader更改为不是 static 并添加一个将IWebHostEnvironment作为参数的构造函数,该参数将保存在MyClassDownloader内的变量中。 然后,需要在Startup.ConfigureServices()中注册MyClassDownloader 最后,在所有MyClassDownloader.GetGoogleCredential()的地方(例如在控制器中), MyClassDownloader的实例都需要可用。 该实例通过将其注入到使用类(例如控制器)的构造函数中而可用。

示例(我没有测试该代码):

// MyClassDownloader.cs
// Make MyClassDownloader non static and add constructor
public class MyClassDownloader 
{
    private readonly IWebHostEnvironment webHostEnvironment;

    public MyClassDownloader(IWebHostEnvironment webHostEnvironment) 
    {
        this.webHostEnvironment = webHostEnvironment;
    }

    // Method not static any more
    public GoogleCredential GetGoogleCredential()
    {    
        // Depending on the hosting OS you might have to use backslash but I'm
        // not sure about that
        string path = System.IO.Path.Combine(
            webHostEnvironment.WebRootPath, 
            "/PrivateKey/myprivatekey.json");

        GoogleCredential credential;
    
        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            credential = GoogleCredential.FromStream(stream).CreateScoped(new[] { "https://www.googleapis.com/auth/indexing" });
        }
    
        return credential;
    }
}

// Startup.cs
public class Startup 
{
    // ... some code

    public void ConfigureServices(IServiceCollection services)
    {
        // ... some code
        
        // Register newly non static MyClassDownloader so it can be injected
        // into classes that need it.
        services.AddTransient<MyClassDownloader>();

        // ... some more code
    }

    // ... some more code
}

// GoogleCredentialConsumer.cs
// Sample class that needs an instance of GoogleCredential. 
// To obtain the GoogleCredential instance, it needs a MyClassDownloader. 
// Might be a controller or another class
public class GoogleCredentialConsumer 
{
    private readonly MyClassDownloader myClassDownloader;

    public GoogleCredentialConsumer(MyClassDownloader myClassDownloader)
    {
        this.myClassDownloader = myClassDownloader;
    }

    public void MethodThatNeedsGoogleCredential()
    {
        var theGoogleCredential = myClassDownloader.GetGoogleCredential();
        // yay, finally I have it!
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM