gpt4 book ai didi

c# - MEF2 和 .NET 核心

转载 作者:行者123 更新时间:2023-12-03 19:46:12 25 4
gpt4 key购买 nike

我一直在阅读关于 MEF 还活着并且很好等等......
我也读到我只需要更换 void ConfigureServices(IServiceCollection services)IServiceCollection ConfigureServices(IServiceCollection services)提供我自己的容器(在我的情况下是 MEF 容器)...

我的问题是我无法找到正确的 return .你看,CompositionHost不实现 IServiceCollection ...

谁能教我如何在 .NET Core webapi 上使用 MEF2 作为我的容器?

最佳答案

这看起来很旧,我希望还为时不晚:D
您需要执行以下操作:
先决条件:Eml.MefBootstrapper

  • 实现 IControllerActivator
  • //using System.Composition.Hosting;
    //using Microsoft.AspNetCore.Mvc;
    //using Microsoft.AspNetCore.Mvc.Controllers;
    public class MefControllerActivator : IControllerActivator
    {
    private readonly CompositionHost container;

    public MefControllerActivator(CompositionHost c) { container = c; }

    public object Create(ControllerContext c) => container.GetExport(c.ActionDescriptor.ControllerTypeInfo.AsType());

    public void Release(ControllerContext c, object controller) { }
    }
  • 创建您的扩展方法。 sample :
  • //using Eml.ClassFactory.Contracts;
    //using Microsoft.AspNetCore.Hosting;
    //using Microsoft.AspNetCore.Mvc.Controllers;
    //using Microsoft.Extensions.DependencyInjection;
    //using Microsoft.Extensions.Logging;
    //using System;

    public static class MefService
    {
    /// <summary>
    /// Call this in Startup.ConfigureServices.
    /// Use Mef to resolve controllers. Hooks with ApplicationStopping for Mef disposal.
    /// Already have a reference to Eml.MefBootstrapper. Remove references to Eml.MefBootstrapper when using this to avoid version mismatch.
    /// </summary>
    /// <param name="serviceCollection"></param>
    /// <param name="bootstrapper"></param>
    public static IClassFactory AddMef(this IServiceCollection serviceCollection, Func<IClassFactory> bootstrapper)
    {
    var serviceProvider = serviceCollection.BuildServiceProvider();

    // Create Mef container
    var classFactory = bootstrapper();

    //Register MefControllerActivator
    serviceCollection.AddSingleton<IControllerActivator>(new MefControllerActivator(classFactory.Container));

    //Graceful exit
    var appLifetime = serviceProvider.GetService<IApplicationLifetime>();
    var loggerFactory = serviceProvider.GetService<ILoggerFactory>();

    appLifetime.ApplicationStopping.Register(() =>
    {
    var logger = loggerFactory.CreateLogger("Global logger");

    logger.LogDebug("Disposing Mef...");
    Eml.Mef.ClassFactory.Dispose(classFactory);
    });

    return classFactory;
    }
    }

  • 创建要在 Startup.cs 中使用的 IServiceCollection 扩展
  • //using Eml.ClassFactory.Contracts;
    //using Eml.Mef;
    //using Microsoft.Extensions.DependencyInjection;
    //using Microsoft.Extensions.Logging;
    //using System;
    //using System.Collections.Generic;
    //using System.Composition.Hosting;
    //using System.Composition.Hosting.Core;
    //using Trackinator.Api.Helpers.Mef;
    //using Trackinator.Infrastructure;

    public static class MefStartup
    {
    public static IClassFactory ConfigureMefService(this IServiceCollection services, ILoggerFactory loggerFactory)
    {
    return services.AddMef(() =>
    {
    // Register instances as shared.
    var instanceRegistrations = new List<Func<ContainerConfiguration, ExportDescriptorProvider>>
    {
    r => r.WithInstance(loggerFactory)
    };

    // Create Mef container
    //return Bootstrapper.Init(Constants.ApplicationId, instanceRegistrations);
    var classFactory = Bootstrapper.Init(new List<string> { Constants.ApplicationId, "Microsoft.AspNetCore.SignalR." }, instanceRegistrations);

    return classFactory;
    });
    }
    }

  • 将此行添加到 Startup.cs
  • public static IClassFactory ClassFactory { get; private set; }

    public void ConfigureServices(IServiceCollection services)
    {
    //..other codes
    ClassFactory = services.ConfigureMefService(LoggerFactory);

    }
    查看示例实现 here
    祝你好运!

    关于c# - MEF2 和 .NET 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39482343/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com