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

时间:2019-10-30 19:46:16

标签: c# dependencies core code-injection blazor

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

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

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

我一直在尝试注入,在启动文件中添加服务以及无格式的各种格式更改。

在启动文件中配置服务:

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”时。

0 个答案:

没有答案