Asp.NETCore使用服务描述符进行验证时出现错误。
创始人
2024-09-18 06:31:08
0

问题通常由以下两个原因引起:

1.服务描述符中存在语法错误。 2.在服务之间有循环依存关系。

在以下代码示例中,我们将检查服务描述符的语法以及是否存在循环依存:

Startup.cs文件:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication1
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            //services.Add ** 服务说明符语法错误**
            //services.AddSingleton(s => new ServiceA(s.GetService()));
            services.AddSingleton(s => new ServiceB(s.GetService()));
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

    public class ServiceA
    {
        public ServiceA(ServiceB b)
        {

        }
    }

    public class ServiceB
    {
        public ServiceB(ServiceA a)
        {

        }
    }
}

在上述示例中,我们尝试注入一个具有语法错误的服务描述符以及存在循环依存关系的代码。这会导致启动时抛出错误:“Error while validating the service descriptor”。要解决这个问题,我们需要修复服务描述符中的语法及处理任何循环依存关系。

修复服务描述符中的语法错误后,可以使用以下代码来检查依存关系的循环,并在找到循环依存关系时引发错误:

相关内容

热门资讯

传授开挂!hhpoker买挂,... 传授开挂!hhpoker买挂,陕麻圈脚本辅助,开挂(透视)辅助工具(有挂教学);亲,有的,ai轻松简...
辅助开挂!牵手辅助神器下载,w... 辅助开挂!牵手辅助神器下载,wepoker私人局辅助器怎么用(透视)开挂辅助下载(讲解有挂)相信很多...
辅助开挂!winner辅助软件... 辅助开挂!winner辅助软件,aapoker俱乐部靠谱吗(透视)开挂辅助插件(有挂工具);亲,有的...
开挂辅助!潘潘讲故事辅助器,德... 开挂辅助!潘潘讲故事辅助器,德普之星透视辅助软件(透视)开挂辅助神器(存在有挂);1、让任何用户在无...
曝光辅助!来玩德州破解器,指尖... 曝光辅助!来玩德州破解器,指尖辅助,开挂(透视)辅助软件(有挂秘籍);亲,有的,ai轻松简单,又可以...
开挂辅助!天天爱柳州辅助,we... 开挂辅助!天天爱柳州辅助,wepoker私人辅助器(透视)开挂辅助插件(有挂讲解);1、超多福利:超...
教会辅助!hhpoker有后台... 教会辅助!hhpoker有后台操作吗,吉祥填大坑有什么诀窍,开挂(透视)辅助平台(真的有挂);1、这...
开挂辅助!乐酷大厅怎么安装,h... 开挂辅助!乐酷大厅怎么安装,hhpoker怎么开透视(透视)开挂辅助脚本(有挂神器);1、这是跨平台...
技术分享"游戏辅助器... 技术分享"游戏辅助器免费版"开挂(透视)wepoker怎么设置透视(真是有挂);软件透视开挂更新新赛...
传授辅助!wepokerplu... 传授辅助!wepokerplus辅助,兴动互娱辅助工具如何下载,开挂(透视)辅助插件(确实有挂);兴...