相比AppDomain.GetAssemblies()
,BuildManager.GetReferencedAssemblies()
(System.Web.Compilation.BuildManager)似乎是一种更可靠的方式来获得由在运行ASP.NET应用程序中引用的程序集,因为AppDomain.GetAssemblies()只得到"已经将组件已经被加载到此应用程序域的执行上下文".
迭代所有程序集是在DI容器中应用程序启动时动态注册类型的重要工具,特别是在应用程序启动期间,可能还有其他程序集未加载(不需要),组合根是第一个需要它们的人.因此,有一个可靠的方法来获取应用程序的引用程序集非常重要.
虽然BuildManager.GetReferencedAssemblies()
对于ASP.NET应用程序来说是一种可靠的方法,但我想知道:有哪些替代方案可用于其他类型的应用程序,例如桌面应用程序,Windows服务和自托管WCF服务?
我正在使用 .NET 5,我想使用IHostedService
类运行后台任务,如您所见:
public class PasargadJobs : IHostedService, IDisposable
{
private Timer _timer = null!;
readonly ITerminalService _terminalService;
readonly IMessageService _messageService;
readonly IMerchantService _merchantService;
public PasargadJobs(
IMerchantService merchantService,
ITerminalService terminalService,
IMessageService messageService)
{
_messageService = messageService;
_terminalService = terminalService;
_merchantService = merchantService;
}
//other parts of code are removed for short code
}
Run Code Online (Sandbox Code Playgroud)
所以我必须将它注入到服务集合中,如您所见:
services.AddHostedService<PasargadJobs>();
Run Code Online (Sandbox Code Playgroud)
但添加此后我收到此错误:
System.AggregateException:“无法构造某些服务(验证服务描述符时出错”ServiceType:Microsoft.Extensions.Hosting.IHostedService Lifetime:Singleton ImplementType:MIMS.Portal.ScheduleJobs.PasargadJobs”:无法使用作用域服务“Microsoft” .EntityFrameworkCore.DbContextOptions`1[MIMS.Portal.Infrustruct.Repositories.AppDbContext]'来自单例'Microsoft.Extensions.Hosting.IHostedService'。)'
这是我的startup.cs
services.AddExpressiveAnnotations();
//--- DB Context ---//
services.AddTransient<AppDbContext, AppDbContext>();
//--- Repositories ---//
services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddTransient<IMerchantRepository, MerchantRepository>();
services.AddTransient<IBankAccountRepository, BankAccountRepository>();
services.AddTransient<IBankRepository, …
Run Code Online (Sandbox Code Playgroud) dependency-injection background-task asp.net-core asp.net-core-hosted-services servicecollection
我正在使用Oracle提供程序实体框架(beta),我遇到了一个问题.
我们的表有Id列,在StoreGeneratedPattern中设置为Identity.我认为EF会自动执行"基础工作",例如创建序列,并为我添加到表中的每条记录获取新标识.但是当我运行代码来添加新记录时,例如:
var comment = new Comment
{
ComplaintId = _currentComplaintId,
Content = CommentContent.Text,
CreatedBy = CurrentUser.UserID,
CreatedDate = DateTime.Now
};
context.Comments.AddObject(comment);
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
一个异常仍然会抛出,这是
{"ORA-00001:违反了唯一约束(ADMINMGR.CONSTRAINT_COMMENT)"}
(CONSTRAINT_COMMENT是约束要求注释标识必须是唯一的.
我该如何解决这个问题?
非常感谢你!
"世界上最快的IOC容器......一个静态预编译的IOC容器,其运行速度与没有IOC容器的应用程序一样快".
它仍然是今天最快的IOC容器吗?它准备好生产吗?在编译时有没有其他容器可以做IOC?与其他IOC容器相比,它的主要优点和缺点是什么?
谢谢
我正在编写自己的范围(即实现的类org.springframework.beans.factory.config.Scope
),我需要注入一些bean.我怎么做?
背景:Spring必须首先创建所有范围bean,以便我可以定义哪些bean进入范围.但是,首先需要构建范围的bean呢?
是否可以有条件地在其他组件的状态上注册组件?就像是:
ContainerBuilder.RegisterConditionally<T>(
Func<IComponentContext, bool>,
Func<IComponentContext, T>);
Run Code Online (Sandbox Code Playgroud)
我发现在autofac的V2之前,可以使用一个Register().OnlyIf()
看起来像我正在寻找的结构.我希望此功能有条件地覆盖默认注册.
class CommonRegistrations
{
public virtual void Register(ContainderBuilder builder)
{
builder.Register(ctx => LoadSettings()).As<ISettings>().SingleInstance();
builder.RegisterType<DefaultFoo>().As<IFoo>();
}
}
class SpecificRegistrations : CommonRegistrations
{
public virtual void Register(ContainerBuilder builder)
{
base.Register(builder);
builder.ConditionalyRegister(
ctx => ctx.Resolve<ISettings>().ReallyUseSpecificFoo,
ctx => new SpecificFoo()).As<IFoo>();
}
}
...
var builder = new ContainerBuilder();
var registrations = new SpecificRegistrations();
registrations.Register(builder);
var container = builder.Build();
IFoo foo = container.Resolve<IFoo>();
Run Code Online (Sandbox Code Playgroud)
foo将根据ISettings.ReallyUseSpecificFoo
实例DefaultFoo
或实例SpecificFoo
.
谢谢.
我正在试图弄清楚如何使用Simple Injector,我已经在项目周围使用它,没有注册简单服务及其组件的问题.
但是,我希望在具有两个以上实现接口的构造函数的组件时使用依赖注入器.
public DAL: IDAL
{
private Logger logger;
string _dbInstance;
public DAL()
{
logger = new Logger();
}
public DAL(string databaseInstance)
{
logger = new Logger();
_dbInstance = databaseInstance;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我注册服务的方式:
container.Register<IDAL, DAL>();
Run Code Online (Sandbox Code Playgroud)
运行代码,这是发生的错误:
要使容器能够创建DAL,它应该只包含一个公共构造函数,但它有2个.
删除构造函数后,下一个错误是它不允许我的构造函数接受参数.
DAL类型的构造函数包含String类型的参数'databaseInstance',它不能用于构造函数注入.
有什么方法我可以在类有2个以上的公共构造函数的情况下进行依赖注入吗?或者有一个接受参数的公共构造函数?
我在这里阅读文档:SimpleInjector(入门)
该文档开始易于理解,但它会呈指数级复杂,如果他们提到的后一个例子中的任何一个与我的问题有关,我就很难解读.
c# dependency-injection inversion-of-control constructor-injection simple-injector
我在Unity中有一组Sprite对象.它们的大小取决于加载的图像.我想将它们像平铺地图并排组合成一个图像.我希望它们的布局就像你正在形成一排图像,一个接一个.(注意:没有一个在另一个之上)我怎么能这样做?
我正在组合的原因(仅适用于那些想知道的人)是因为我正在使用polygon2D Collider.由于当我并排使用多个碰撞器时发生了一些奇怪的行为,我决定在添加一个大的多边形碰撞器之前组合图像.请注意,这些事情发生在运行时.我不能只创建一个大图像并加载,因为图像的顺序只在运行时确定.
我希望能得到一些帮助.谢谢.
简单的注入器文档提供了有关如何为WebRequest,Web API,WCF设置容器的很好示例......但这些示例一次特定于一种技术/生活方式.我们的Web应用程序大部分使用它们!我不清楚如何配置容器以适应多种生活方式.
假设我有一个带有Web API的MVC项目.我有以下对象:
我应该为每种生活方式创建和配置一个容器吗?
当我注册的所有内容RegisterPerWebRequest<T>
都适用于两种类型的控制器.这样安全吗?或者在Web API控制器中使用async/await时会遇到麻烦吗?
当我同时注入相同实例的MVC和Web API控制器时,最佳配置是什么?
我应该使用混合生活方式吗?
现在让事情变得复杂......我们的应用程序也使用后台任务和SignalR.
这些都有时会发生在WebRequest之外,并且需要访问如上所述的相同对象.
最好的解决方案是使用Lifetime范围?
我需要为这种生活方式创造一个新的容器吗?或者我可以重用/重新配置我的MVC/Web API容器吗?
有三重生活方式吗?
我对使用Autofac的实现中的Dispose()
方法有点困惑IDisposable
说我的对象有一定的深度:
Controller
取决于IManager
;Manager
取决于IRepository
;Repository
取决于ISession
;ISession
是IDisposable
.这导致以下对象图:
new Controller(
new Manager(
new Repository(
new Session())));
Run Code Online (Sandbox Code Playgroud)
我是否还需要使我的Manager和Repository实现IDisposable,并在Controller中调用Manager.Dispose(),在Manager中调用Repository.Dispose()等,或者Autofac会自动知道我的调用堆栈中哪些对象需要正确处理?Controller对象已经是IDisposable,因为它派生自基本ASP.NET Web API控制器
.net dependency-injection idisposable inversion-of-control autofac
c# ×5
.net ×3
autofac ×2
asp.net ×1
asp.net-core ×1
asp.net-core-hosted-services ×1
asp.net-mvc ×1
idisposable ×1
java ×1
ora-00001 ×1
oracle ×1
performance ×1
scope ×1
spring ×1
sprite ×1
texture2d ×1
textures ×1
wcf ×1
winforms ×1