我有以下项目结构:
- 项目服务器
- 项目.客户
- 项目应用程序
- Poroject.Shared
我想创建ExportManager,用于使用closedXML库将数据导出到Excel。所以我在Project中创建了以下文件。应用程序:
IExportManager.cs:
public interface IExportManager
{
bool TryExportToExcelFile(DataTable dataTable, string excelTemplatePath, IJSRuntime js, string selectedProjectName, out FileExportErrorInfo errorInfo);
}
ExportManager.cs:
public class ExportManager : IExportManager
{
/// <summary>
/// Method for exporting DataTable to Excel
/// </summary>
public bool TryExportToExcelFile(DataTable dataTable, string excelTemplatePath, IJSRuntime js, string selectedProjectName, out FileExportErrorInfo errorInfo)
{
errorInfo = null;
if (string.IsNullOrEmpty(excelTemplatePath))
{
return false;
}
string worksheetName = "Data";
WriteToWorkbook(dataTable, excelTemplatePath, worksheetName, js, selectedProjectName, ref errorInfo);
return errorInfo.State == FileExportErrorState.Successful;
}
private static void WriteToWorkbook(DataTable dataTable, string excelTemplatePath, string worksheetName, IJSRuntime js, string selectedProjectName, ref FileExportErrorInfo errorInfo)
{
SaveOptions? saveOptions = new SaveOptions()
{
EvaluateFormulasBeforeSaving = true,
};
using XLWorkbook workbook = File.Exists(excelTemplatePath)
? new XLWorkbook(excelTemplatePath)
: new XLWorkbook();
...
using (MemoryStream stream = new MemoryStream())
{
workbook.SaveAs(stream);
byte[] content = stream.ToArray();
js.InvokeAsync<object>("saveAsFile", fileName, Convert.ToBase64String(content));
}
}
}
然后我将在Project.Server->中注册此服务;Startup.cs with:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddSingleton<IExportManager, ExportManager>();
}
然后在Project.Client中,在一个剃刀页面上,我给它注入了:
[Inject] protected IExportManager ExportManager { get; set; }
然后我正在运行我的应用程序,并继续进行上述注入的网页,并得到以下错误:
Microsoft.AspNetCore.Components.WebAssembly.Frendering.WebAssemblyRenderer[100]呈现组件时出现未处理的异常:无法为类型的属性"ExportManager"提供值"Project.Client.Pages.Project.Project"。没有注册的服务类型为"Project.Application.IExportManager"。System.InvalidOperationException:无法为属性提供值类型"Project.Client.Pages.Project.Project"上的"ExportManager"。在那里不是"Project.Application.IExportManager"类型的注册服务。在Microsoft.AspNetCore.Components.ComponentFactory。<gt;c__DisplayClass7_0.g__初始化|1(IServiceProviderserviceProvider、IComponent组件(Microsoft.AspNetCore.Components.ComponentFactory.PerformPropertyInjection(IServiceProviderserviceProvider,IComponent实例(Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProviderserviceProvider,类型componentType(Microsoft.AspNetCore.Components.RenderTree.Render.InstantiateComponent(类型componentType(Microsoft.AspNetCore.Components.RenderTree.Prenderer.InstantiateChildComponentOnFrame(RenderTreeFrame&帧,Int32 parentComponentId(Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext&diffContext,Int32 frameIndex(Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext&diffContext,Int32 frameIndex(Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext&diffContext,Int32 newFrameIndex(
我做错了什么?
附言:如果以上信息不够,我可以尝试将示例项目上传到GitHub。
这是我的项目。应用程序.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.4" />
<PackageReference Include="Microsoft.JSInterop" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..BlazingProjectProject.SharedProject.Shared.csproj" />
</ItemGroup>
</Project>
简单的答案是在服务器项目中添加服务,然后尝试在客户端项目中使用它。通常的答案是在客户端项目中设置服务。
然而,这是行不通的。我可以看到在客户端上无法运行的文件系统操作。您需要重新设计并使用API调用从服务器生成和提供工作簿。
为什么要将它注册到服务器项目而不是客户端?