我正在使用ASP.NET MVC和Google Big Query。我正在使用Google.Apis.Bigquery.v2 nuget包。
我正在使用
创建BigqueryService对象的实例 private async Task<BigqueryService> GetBigqueryServiceAsync()
{
var ms = new MemoryStream();
var filePath = _pathService.MapPath("~/App_Data/bigquery.p12");
await _fileService.CopyToStreamAsync(filePath, ms);
var certificate = new X509Certificate2(ms.GetBuffer(), _settings.GoogleCloudServicePassword, X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(_settings.GoogleCloudServiceEmail)
{
Scopes = new[] {BigqueryService.Scope.Bigquery, BigqueryService.Scope.CloudPlatform}
}.FromCertificate(certificate));
var bigqueryService = new BigqueryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
这种方法有效,但问题是每当我需要调用Google Big Query时,我需要执行这个方法,这对所有请求都是一样的,所以我认为我应该让它成为单例。但是当我尝试使用Lazy失败时,
static readonly Lazy<BigqueryService> BigqueryServiceInstance = new Lazy<BigqueryService>(() => GetBigqueryServiceAsync);
它显示了编译错误。注意我的_pathService是intance变量。