![](/img/trans.png)
[英].net core 3.1: 'IAsyncEnumerable<string>' does not contain a definition for 'GetAwaiter'
[英].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.