在Blazor中尝试使用依赖注入激活时无法解析服务类型

问题描述 投票:0回答:1

我有一个处理PDF的检索和合并的服务项目,还有一个具有上下文和模型的存储库项目。

最初,我们使用具有功能性的API来创建它,并认为对于简单的应用程序来说这会产生过多的开销,并将逻辑放入简单的服务中并对其进行引用。

我正在尝试从Blazor页面调用服务中的方法,我已经在启动文件中注册了Service,并在页面上为DI注入了Services,但现在我收到以下错误。具体而言,是与服务所引用的存储库中的上下文有关。

我一直在尝试注入,在启动文件中添加了服务,并且各种格式更改都无济于事。

在启动文件中配置服务:

public void ConfigureServices(IServiceCollection services)
    {

        services.AddTransient<IBOLService, BOLService>();
        services.AddTransient<IPDFService, PDFService>();
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddHttpClient();
        services.AddDbContext<CoviaDevIntegrationControlContext>(options =>
              options.UseSqlServer(Configuration.GetConnectionString("IntegrationControl")));
       }

剃须刀主页面的注入和使用摘录:

@page "/download"
@using LAS.BOLDownloader.Models
@using System.IO;
@using System.Net.Http;
@using System.Net.Http.Headers
@using Microsoft.AspNetCore.Components.Web
@using LAS.BOLDownloader.Data;
@using BOL.Data;
@using BOL.Services;


@inject IJSRuntime js

@inject Microsoft.JSInterop.IJSRuntime JS
@inject CoviaDevIntegrationControlContext IntegrationControlContext

@inject IHttpClientFactory clientFactory

@inject IBOLService _bolService 
@inject IPDFService _pdfService

服务类别:

public class BOLService : IBOLService
    {
        private CoviaDevIntegrationControlContext _context;

        public BOLService(CoviaDevIntegrationControlContext context)
        {
            _context = context;
        }

        public async Task<byte[][]> GetDocuments(string searchString, string IdType, int SiteId)
        {

            var bolNumbers = searchString.Split(',');

            if (IdType == "BolNumber")
            {
                return await _context.Las2cpBolstaging.Where(o => bolNumbers.Contains(o.BolNumber.ToString()) && o.PlantId == SiteId).Select(o => o.BolDoc).ToArrayAsync();


            }
            else if (IdType == "ShipmentId")
            {
                return await _context.Las2cpBolstaging.Where(o => bolNumbers.Contains(o.BolNumber.ToString()) && o.PlantId == SiteId).Select(o => o.BolDoc).ToArrayAsync();
            }
            else
            {
                return null;
            }

        }

存储库中的上下文:

public partial class CoviaDevIntegrationControlContext : DbContext
    {
        public CoviaDevIntegrationControlContext()
        {
        }

        public CoviaDevIntegrationControlContext(DbContextOptions<CoviaDevIntegrationControlContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Las2cpBoldocStaging> Las2cpBoldocStaging { get; set; }
        public virtual DbSet<Las2cpBolstaging> Las2cpBolstaging { get; set; }
        public virtual DbSet<LasBols> LasBols { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Server=dev-sql1;Database=Covia.Dev.IntegrationControl;Trusted_Connection=True;MultipleActiveResultSets=True;");
            }
        }

使用注入的功能:

 protected async Task GetBOL(int SiteId, string IdType)
    {
        var client = clientFactory.CreateClient();

        si.SiteId = SiteId;
        si.IdType = IdType;


        try
        {

            Process bp = new BOLProcess( _bolService, _pdfService);

            var bolPdf = bp.GetBOLs(si.searchString, si.IdType, si.SiteId).Result;

            bolResultsList = bp.GetResults(si.searchString, si.IdType, si.SiteId).Result;

            if (bolPdf != null && bolPdf.Length > 0)

            {

                await FileUtil.SaveAs(js, "BOLDocument.pdf", bolPdf);
            }

        }
        catch (Exception ex)
        {
            var error = ex.Message;
        }

    }

InvalidOperationException:验证服务时出错描述符'ServiceType:BOL.Services.IBOLService寿命:瞬态ImplementationType:BOL.Services.BOLService':无法解析类型服务“ BOL.Repository.Models.CoviaDevIntegrationControlContext”时尝试激活“ BOL.Services.BOLService”。

内部异常InvalidOperationException:无法解析服务用于类型'BOL.Repository.Models.CoviaDevIntegrationControlContext'尝试激活“ BOL.Services.BOLService”时。

c# dependencies core code-injection blazor
1个回答
0
投票

尝试此补救方法:

由于DBContext受作用域限制,所以在services.AddTransient<IBOLService, BOLService>();中使用AddScoped而不是AddTransient

如果不是问题,请尝试放置

 services.AddTransient<IBOLService, BOLService>();
        services.AddTransient<IPDFService, PDFService>();

在ConfigureServices的末尾,而不是在开头...

您还应该提供baseUri,因为这是SPA应用程序,对:

services.AddHttpClient<IXXXHttpClient, XXXHttpClient>(config =>
         config.BaseAddress = new Uri(<uri string>));

希望这有帮助...

© www.soinside.com 2019 - 2024. All rights reserved.