我使用的是AspNetCore 2.2,这里的文档如下(无门):
我使用Autofac,我的Startup类有以下方法:
public void ConfigureServices(IServiceCollection services)
public void ConfigureContainer(ContainerBuilder containerBuilder) //this is where things can be registered directly with autofac and runs after ConfigureServices
public void Config
我们有一个现有的MVC3项目,并为DI使用Autofac。
为了提供RESTful服务,我们打算使用ServiceStack。我们如何让ServiceStack使用已有的初始化autofac的Bootstrapper.cs?
我们现有的Bootrapper.cs:
public static class Bootstrapper
{
public static ContainerBuilder builder;
public static void Initialise()
{
builder = new Con
我在我的项目中有一个autofac DI。我希望通过常规方式公开一个接口,我的项目的所有其他接口都将从该接口继承。是否可以在启动时自动注册继承接口的组件?例如:
Public interface IConvetionInterface {}
public interface IImplementationA:IConvetionInterface
{
public void DoSomethingA();
}
public interface IImplementationB:IConvetionInterface
{
public void DoSomethingB();
}
通过构造
我有一个类,它使用AutoFac从二进制文件夹中的程序集中构建容器。这实际上是在dll上进行迭代,并使用接口注册类:
private static void RegisterAssembly(ContainerBuilder builder, string assemblyNamePattern)
{
// Get the path to the currently executing assembly. We will load dll's from here only.
var path = Path.GetDirectoryName
我试图让SignalR与Autofac一起工作。我对我在这里所做的事情做了一个简化版本的回购:
这是改编自使用GlobalHost的工作。
使用hte GlobalHost对象可以很好地工作。我试图跟踪Autofac网站上关于如何注入SignalR服务的文档,但我无法让它工作。这是用于注册我的依赖项的配置文件:
public static IContainer RegisterDependencies()
{
// Create your builder.
var builder = new ContainerBuilder();
我使用一个动态而灵活的插件系统来维护遗留代码,它被实现为一个非常浅的类树,其中所有的具体插件都扩展了抽象的Plugin类。这些插件中的大多数都是具体的类(如LoadSound、HighPassFilter),但其中一些有一个泛型类型参数(如CreateCopy<T>)。
这个系统的初始化序列基本上是加载所有程序集,列出它们的所有类型,选择Plugin的后代程序集,然后迭代它们并将它们插入到一种自制的IoC容器中。
为了提高这个系统的可维护性,我想用Autofac代替自制的IoC容器,Autofac在代码库的其他地方使用。
我已经设法以一种有用的方式注册了所有类型,如下所示:
Aut
我试图使用基本类型的IEnumerable解析所有的泛型,甚至不确定这是否可能.我正在寻找一种解决方案,在这种解决方案中,我不需要依赖autofac容器,因为我没有访问它的。请参阅下面的代码,只需将粘贴复制到您的Program.cs文件中,您将需要一个对Autofac的引用来查看我需要的内容:)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Autofac;
namespace A
无法使用KeyFilter属性解析asp.net核心web api 2.2中的注册类型
我想使用Autofac来解析一个类型的不同实例。我使用RegisterType().Keyed和RegisterType().Named在Startup.ConfigureServices中注册了类型。
我可以使用IContainer.ResolveKeyed来解析实例,但不能在构造函数中使用KeyFilter来解析实例
启动
public class Startup
{
public Startup(IConfiguration configuration)
在我学习的ASP.NET MVC 4应用程序中,我使用存储库模式和服务层。该项目使用实体框架和Autofac。我的数据类很简单,几乎所有的操作都是基本的CRUD操作。
我有一个抽象存储库库,如下所示:
public abstract class RepositoryBase<T> where T : class
这是实体Sample1的一个示例存储库:
public class Sample1Repository : RepositoryBase<Sample1>, ISample1Repository
{
public Sample1Rep
public class Sub1 : Base<SomeClass>
{
// for sake of getting injection to work, not injecting anything in ctor
public Sub1() {}
....
}
public class Sub2 : Base<SomeOtherClass>
{
// for sake of getting injection to work, not injecting anything in ctor
public Sub2() {}
.
在ASP.NET Core2.0中,我在Startup类上使用了ConfigureServices方法来连接Autofac,包装现有的services注册并添加其他注册。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Standard service registrations (ex: services.AddMvc())
// ...
// Autofac
var builder = new ContainerBuilder();
bu
我有一个定义如下的类:
public class CommandBase<T>
{
public string CommandText { get; set; }
}
public class CommandService : ICommandService
{
public CommandService(CommandBase<SomeClass> command)
{
//some other code
}
}
是否可以使用Autofac创建一个配置,以便当CommandService的构造函数参数为CommandB
我要在一个单元测试中实例化这个类:
public class Customer
{
internal Customer(Guid id) {
// initialize property
}
}
如果我使用new Customer()实例化来自另一个(单元测试)程序集的测试类,因为我添加了[assembly: InternalsVisibleTo("MyProject.Tests")]
var sut = new Customer(Guid.NewGuid()); // works
但是当我在另一个(单元测试)程序集中设置一个autofac容器时
public class A
{
Func<B> _b;
public A(Func<B> b) { _b = b; }
public void M()
{
var b = _b();
b.DoSomething();
}
}`
当我解决它抛出错误时,理论上是否有可能用旧版本的Autofac动态实例化?如果不是,什么才是解决之道?