Castle.Windsor - 为什么名称需要与界面相似?
Castle.Windsor - why does the name needs to be similar to the interface?
我在 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));
当我尝试在本地进行测试时,出现以下错误:
抛出异常:HangFire.Core.dll 中的 "Castle.MicroKernel.ComponentNotFoundException"
找不到支持服务 ICsvExportService 的组件。
尝试不同解决方案 30 分钟后,我将文件 ExportService 重命名为 CsvExportService,奇迹发生了!成功了!
谁能解释一下为什么我需要使用与接口相同的名称才能使 DI 容器识别实际实现 class?
Castle.Core .NET 4.5 版本为 3.3.3
Castle.Windsor .NET 4.5 的版本是 3.3.0
注册码如下:
container.Register(
Classes.FromThisAssembly()
.Where(type => type.Name.EndsWith("Service"))
.WithServiceDefaultInterfaces()
.Configure(c => c.LifestyleTransient()));
非常感谢。
您没有说明您是如何注册接口的,类,但很可能您使用的是 DefaultInterfaces
约定。
This method performs matching based on type name and interface's name. Often you'll find that you have interface/implementation pairs like this: ICustomerRepository
/CustomerRepository
, IMessageSender
/SmsMessageSender
, INotificationService
/DefaultNotificationService
. This is scenario where you might want to use DefaultInterfaces
method to match your services. It will look at all the interfaces implemented by selected types, and use as type's services these that have matching names. Matching names, means that the implementing class contains in its name the name of the interface (without the I on the front).
有许多不同的约定,但您可能只是在寻找 AllInterfaces
:
When a component implements multiple interfaces and you want to use it as a service for all of them, use WithService.AllInterfaces()
method.
我在 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));
当我尝试在本地进行测试时,出现以下错误:
抛出异常:HangFire.Core.dll 中的 "Castle.MicroKernel.ComponentNotFoundException" 找不到支持服务 ICsvExportService 的组件。
尝试不同解决方案 30 分钟后,我将文件 ExportService 重命名为 CsvExportService,奇迹发生了!成功了!
谁能解释一下为什么我需要使用与接口相同的名称才能使 DI 容器识别实际实现 class?
Castle.Core .NET 4.5 版本为 3.3.3 Castle.Windsor .NET 4.5 的版本是 3.3.0
注册码如下:
container.Register(
Classes.FromThisAssembly()
.Where(type => type.Name.EndsWith("Service"))
.WithServiceDefaultInterfaces()
.Configure(c => c.LifestyleTransient()));
非常感谢。
您没有说明您是如何注册接口的,类,但很可能您使用的是 DefaultInterfaces
约定。
This method performs matching based on type name and interface's name. Often you'll find that you have interface/implementation pairs like this:
ICustomerRepository
/CustomerRepository
,IMessageSender
/SmsMessageSender
,INotificationService
/DefaultNotificationService
. This is scenario where you might want to useDefaultInterfaces
method to match your services. It will look at all the interfaces implemented by selected types, and use as type's services these that have matching names. Matching names, means that the implementing class contains in its name the name of the interface (without the I on the front).
有许多不同的约定,但您可能只是在寻找 AllInterfaces
:
When a component implements multiple interfaces and you want to use it as a service for all of them, use
WithService.AllInterfaces()
method.