小编And*_*rut的帖子

Castle.Windsor - 为什么名称需要与界面类似?

在我的ASP.NET MVC应用程序中尝试创建新的HangFire作业时,我注意到了这个有趣的场景.

// this is the interface for the HangFire job.
public interface ICsvExportService
{
    void ExportCsvToEmail();
}

// this is the implementation of the above interface.
public class ExportService : ICsvExportService
{
// code goes here.
}


RecurringJob.RemoveIfExists("My CSV exports");
        RecurringJob.AddOrUpdate<ICsvExportService>(
            "Send CSV exports",
            x => x.ExportCsvToEmail(),
            Cron.Daily(8));
Run Code Online (Sandbox Code Playgroud)

当我试图在本地测试时,我收到以下错误:

抛出异常:HangFire.Core.dll中的"Castle.MicroKernel.ComponentNotFoundException"找不到用于支持服务ICsvExportService的组件.

尝试不同的解决方案30分钟后,我重命名了文件:ExportService to CsvExportService,魔术发生了!有效!

有人可以解释为什么我需要使用与接口相同的名称才能使DI容器识别实际的实现类?

Castle.Core版本为3.3.3 for .NET 4.5 Castle.Windsor版本为3.3.0 for .NET 4.5

注册代码如下:

container.Register(
            Classes.FromThisAssembly()
                .Where(type => type.Name.EndsWith("Service"))
                .WithServiceDefaultInterfaces()
                .Configure(c => c.LifestyleTransient()));
Run Code Online (Sandbox Code Playgroud)

非常感谢.

c# asp.net-mvc castle-windsor hangfire

1
推荐指数
1
解决办法
291
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

castle-windsor ×1

hangfire ×1