【发布时间】:2020-09-01 13:32:34
【问题描述】:
我正在使用带有 EF Core 和 Azure 存储(文件存储)的 ASP.NET Core 3.1。我正在使用 Microsoft.Azure.Storage.File version 11.2.2 处理 Azure 存储文件。
除了依赖注入 (DI) 问题之外,我不确定这个错误到底指的是什么?
我遇到的大多数引用此错误消息的文章或 SO 文章都建议使用 DI,但是当他们在 Startup.cs 中注入它时,他们有一个接口来伴随他们的注入。我没有。
我正在为这个视图使用 Razor 页面。这段代码以前是为 MVC 视图编写的,我正在尝试将其转换为内聚。见原帖HERE。 MVC 中的项目可以正常工作。我应该放弃尝试在 Razor 页面中执行所有这些操作并在我的整个项目中使用 MVC,还是我遗漏了一个明显的问题??
这是我的设置:
namespace AzureFileShare.Pages.Files
{
public class IndexModel : PageModel
{
private readonly IConfiguration _configuration;
public IndexModel(
IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<IList<FileModel>> OnGetAsync()
{
string fileStorageConnection = _configuration.GetValue<string>("fileStorageConnection");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("payreports");
CloudFileDirectory root = share.GetRootDirectoryReference();
CloudFileDirectory dir = root.GetDirectoryReference(@"E000002/stubs");
// list all files in the directory
var fileData = await list_subDir(dir);
return fileData;
}
public static async Task<List<FileModel>> list_subDir(CloudFileDirectory fileDirectory)
{
var fileData = new List<FileModel>();
FileContinuationToken token = null;
do
{
FileResultSegment resultSegment = await fileDirectory.ListFilesAndDirectoriesSegmentedAsync(token);
foreach (var fileItem in resultSegment.Results)
{
if (fileItem is CloudFile)
{
var cloudFile = (CloudFile) fileItem;
//get the cloudfile's properties and metadata
await cloudFile.FetchAttributesAsync();
// Add properties to FileDataModel
fileData.Add(new FileModel()
{
FileName = cloudFile.Name,
Size = Math.Round((cloudFile.Properties.Length / 1024f), 2).ToString(),
DateModified = DateTime.Parse(cloudFile.Properties.LastModified.ToString()).ToLocalTime().ToString()
});
}
if (fileItem is CloudFileDirectory)
{
var cloudFileDirectory = (CloudFileDirectory)fileItem;
await cloudFileDirectory.FetchAttributesAsync();
//list files in the directory
var result = await list_subDir(cloudFileDirectory);
fileData.AddRange(result);
}
// get the FileContinuationToken to check if we need to stop the loop
token = resultSegment.ContinuationToken;
}
} while (token != null);
return fileData.OrderByDescending(o => Convert.ToDateTime( o.DateModified)).ToList();
}
}
}
型号
public class FileModel
{
public string FileName { get; set; }
public string Size { get; set; }
public string DateModified { get; set; }
}
appsettings.json
{
"ConnectionStrings": {
"fileStorageConnection": "DefaultEndpointsProtocol=https;AccountName=navraereports;AccountKey=REMOVEDFORPUBLIC;EndpointSuffix=core.windows.net"
}
}
@page
@model List<FileModel>
@{
ViewData["Title"] = "Download Pay Stub Copies";
}
<h1>Pay Stub Copies</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>File Name</th>
<th>File Size</th>
<th>File Date</th>
<th>Download</th>
</tr>
</thead>
<tbody>
@foreach (var data in Model)
{
<tr>
<td>@data.FileName</td>
<td>@data.Size</td>
<td>@data.DateModified</td>
<td>
<a class="btn btn-primary"
href="/File/DownloadStub?id=@data.FileName">Download</a>
</td>
</tr>
}
</tbody>
</table>
堆栈跟踪:
System.InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'System.Collections.Generic.List`1[AzureFileShare.FileModel]'. There should only be one applicable constructor.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& parameterMap)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& parameterMap)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelActivatorProvider.CreateActivator(CompiledPageActionDescriptor actionDescriptor)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelFactoryProvider.CreateModelFactory(CompiledPageActionDescriptor descriptor)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvokerProvider.CreateCacheEntry(ActionInvokerProviderContext context, FilterItem[] cachedFilters)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory.CreateInvoker(ActionContext actionContext)
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory.<>c__DisplayClass7_0.<CreateRequestDelegate>b__0(HttpContext context)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
【问题讨论】:
-
堆栈跟踪肯定会有所帮助。不仅如此,您能否编辑您的代码并删除与问题无关的所有内容。换句话说,你能把你的代码变成minimal, reproducible example吗?
-
谢谢,我希望这会更好......
标签: dependency-injection asp.net-core-mvc