我有以下專案結構:
- 專案服務器
- 專案.客戶
- 專案申請
- 專案共享
我想創建 ExportManager,用于使用 closedXML 庫將資料匯出到 Excel。所以我在 Project.Application 中創建了以下檔案:
IExportManager.cs:
public interface IExportManager
{
bool TryExportToExcelFile(DataTable dataTable, string excelTemplatePath, IJSRuntime js, string selectedProjectName, out FileExportErrorInfo errorInfo);
}
匯出管理器.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 中注冊此服務:
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.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property 'ExportManager' on type 'Project.Client.Pages.Project.Project'. There is no registered service of type 'Project.Application.IExportManager'. System.InvalidOperationException: Cannot provide a value for property 'ExportManager' on type 'Project.Client.Pages.Project.Project'. There is no registered service of type 'Project.Application.IExportManager'. at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass7_0.g__Initialize|1(IServiceProvider serviceProvider, IComponent component) at Microsoft.AspNetCore.Components.ComponentFactory.PerformPropertyInjection(IServiceProvider serviceProvider, IComponent instance) at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType) at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateComponent(Type componentType) at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame& frame, Int32 parentComponentId) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex)
我做錯了什么?
PS 如果以上資訊不夠,我可以嘗試將示例專案上傳到 GitHub。
這是我的 Project.Application.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="..\BlazingProject\Project.Shared\Project.Shared.csproj" />
</ItemGroup>
</Project>
uj5u.com熱心網友回復:
簡單的答案是您在服務器專案中添加服務,然后嘗試在客戶端專案中使用它。正常的答案是在客戶端專案中設定服務。
然而那是行不通的。我可以看到在客戶端上不起作用的檔案系統操作。您需要重新設計并使用 API 呼叫從服務器生成和提供作業簿。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416846.html
標籤:
