C# 在摆脱Autofac的同时添加依赖项服务-开源项目

C# 在摆脱Autofac的同时添加依赖项服务-开源项目,c#,asp.net,.net,.net-core,C#,Asp.net,.net,.net Core,我正试图通过将他们当前的网站迁移到.NETCore来帮助一个开源项目() 我得到了超远,并且完成了所有页面的UI,正如您在GIF中看到的 但是,由于我尚未实现Autofac,因此它无法获取提要(NewCombinedFeedSource.cs)和作者(IAmACommunityMember.cs)数据,而这些数据以前是用来通过Autofac填充所有内容的: builder.RegisterAssemblyTypes(此程序集) .Where(t=>t.IsAssignableTo()) .AsI

我正试图通过将他们当前的网站迁移到.NETCore来帮助一个开源项目()

我得到了超远,并且完成了所有页面的UI,正如您在GIF中看到的

但是,由于我尚未实现Autofac,因此它无法获取提要(NewCombinedFeedSource.cs)和作者(IAmACommunityMember.cs)数据,而这些数据以前是用来通过Autofac填充所有内容的:

builder.RegisterAssemblyTypes(此程序集)
.Where(t=>t.IsAssignableTo())
.AsImplementedInterfaces()
.SingleInstance();
builder.RegisterType()
.AsSelf()
.SingleInstance();
我认为不再需要Autofac了,我想知道我是否可以在我的
Startup.cs的
ConfigureServices
功能中添加一些东西来启动并运行它

我缺少什么,什么资源可以帮助我?我试图通过,但他们似乎做了不同的事情


注意:我添加了这一行
services.AddSingleton()以防止崩溃发生。

我能够使用@Nkosi提到的反射来获取作者列表,而不使用Autofac。我意识到我甚至不需要这些

private IEnumerable<IAmACommunityMember> GetAuthors()
{
    var assembly = Assembly.GetAssembly(typeof(IAmACommunityMember));

    var types = assembly.GetTypes();
    var authorTypes = types.Where(t => typeof(IAmACommunityMember).IsAssignableFrom(t) &&
        !_interfaceNames.Contains(t.Name));

    foreach (var authorType in authorTypes)
    {
        var author = (IAmACommunityMember)Activator.CreateInstance(authorType);
        yield return author;
    }
}

private string[] _interfaceNames =
{
    nameof(IAmACommunityMember),
    nameof(IWorkAtXamarinOrMicrosoft),
    nameof(IAmAXamarinMVP),
    nameof(IAmAMicrosoftMVP),
    nameof(IAmAPodcast),
    nameof(IAmANewsletter),
    nameof(IAmAFrameworkForXamarin),
    nameof(IAmAYoutuber)
};
private IEnumerable GetAuthors()
{
var assembly=assembly.GetAssembly(typeof(iamacommunitymber));
var types=assembly.GetTypes();
var authorTypes=types.Where(t=>typeof(iamacommunitymber).IsAssignableFrom(t)&&
!\u interfaceNames.Contains(t.Name));
foreach(authorTypes中的var authorType)
{
var author=(iamacommunitymber)Activator.CreateInstance(authorType);
收益回报作者;
}
}
私有字符串[]\u接口名称=
{
姓名(IAMAC社区成员),
名称(IWorkAtXamarinOrMicrosoft),
姓名(IAMAXAMARINNMVP),
姓名(IAmAMicrosoftMVP),
姓名(IAmAPodcast),
姓名(IAmANewsletter),
姓名(Xamarin的IAMAFramework),
姓名(IAmAYoutuber)
};

与久经考验的第三方扩展(如Autofac)相比,库存DI有点有限

您仍然可以自己做一些事情,但这需要您使用反射来查找和注册您的类型

虽然我认为您的解决方案适合您,但我建议您仍然向容器注册类型,并由容器管理它们,而不是自己初始化它们

下面是一个简单的例子

public static IServiceCollection AddFeedSource(this IServiceCollection services) {        
    Type serviceType = typeof(IAmACommunityMember);
    Assembly assembly = Assembly.GetAssembly(serviceType);
    IEnumerable<Type> types = from type in assembly.GetTypes()
                where serviceType.IsAssignableFrom(type)
                      && !type.IsAbstract
                      && !type.IsInterface
                select type;

    foreach (Type type in types) {
        services.AddSingleton(serviceType, type);
    }

    services.AddSingleton<NewCombinedFeedSource>();

    return services;
}
启动中
使用默认DI容器

这允许
IEnumerable
用于构造函数注入,就像在中一样

public NewCombinedFeedSource(IEnumerable tamarins){
//...
}
//...

@Nkosi有什么建议吗?我想你想用库存DI?您遇到的错误/问题是什么?与久经考验的第三方扩展(如Autofac)相比,库存DI有点有限。您仍然可以自己做一些事情,但这需要您在使用反射查找和注册您的类型方面做一些工作。@Nkosi非常感谢您的帮助,您指出了正确的方向!我有一个稍微相关的RSS提要问题(很抱歉前面的拼写错误),虽然我认为这对您有效,但我建议您仍然使用容器注册类型,并通过容器管理它们,而不是自己初始化它们。我来看看是否能想出一个简单的例子。这听起来比我的解决方案好得多。会给它一个旋转和更新你
public static IServiceCollection AddFeedSource(this IServiceCollection services) {        
    Type serviceType = typeof(IAmACommunityMember);
    Assembly assembly = Assembly.GetAssembly(serviceType);
    IEnumerable<Type> types = from type in assembly.GetTypes()
                where serviceType.IsAssignableFrom(type)
                      && !type.IsAbstract
                      && !type.IsInterface
                select type;

    foreach (Type type in types) {
        services.AddSingleton(serviceType, type);
    }

    services.AddSingleton<NewCombinedFeedSource>();

    return services;
}
services.AddFeedSource();
public NewCombinedFeedSource(IEnumerable<IAmACommunityMember> tamarins) {

    //...

}

//...