服务结构asp .net核心IoC错误:无法动态代理

时间:2018-07-25 08:57:58

标签: asp.net-core-2.0 autofac azure-service-fabric

我试图在我的api中放入一些IoC并不断收到此错误:

  

不能动态代理Inovatic.SF.Windows.Facade.Facade类型。服务类型一定不能密封,并且对于DynamicProxyGenAssembly2程序集必须可见。这可以通过将类型公开或将InternalsVisibleToAttribute添加到包含该类型的程序集中来实现。例如[程序集:InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]

program.cs:
//[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]
namespace Inovatic.SF.Windows.Facade
{
internal static class Program
{
    /// <summary>
    /// This is the entry point of the service host process.
    /// </summary>
    private static void Main()
    {
        try
        {
            var builder = new ContainerBuilder();
            builder.RegisterModule(new GlobalAutofacModule());
            builder.RegisterServiceFabricSupport();
            builder.RegisterStatelessService<Facade>("Inovatic.SF.Windows.FacadeType");

            using (builder.Build())
            {
                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Facade).Name);
                Thread.Sleep(Timeout.Infinite);
            }
        }
        catch (Exception e)
        {
            ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
            throw;
        }
    }
}

public class GlobalAutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ConfigSettings>();
        builder.RegisterType<PaymentRepository>().As<IPaymentRepository>();
    }
}

}

我已经试过把它放进去了(但不确定应该去哪里):

  

[assembly:InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]

,还尝试将Program类标记为公共类,但这似乎不起作用

编辑:

namespace Inovatic.SF.Windows.Facade
{
internal sealed class Facade : StatelessService
{
    public Facade(StatelessServiceContext context)
        : base(context)
    { 
        var telemetryConfig = TelemetryConfiguration.Active;
        telemetryConfig.InstrumentationKey = Environment.GetEnvironmentVariable("ApplicationInsightsKey");
        FabricTelemetryInitializerExtension.SetServiceCallContext(context);
    }

    /// <summary>
    /// Optional override to create listeners (like tcp, http) for this service instance.
    /// </summary>
    /// <returns>The collection of listeners.</returns>
    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        var endpoints = Context.CodePackageActivationContext.GetEndpoints()
            .Where(endpoint => endpoint.Protocol == EndpointProtocol.Http 
            || endpoint.Protocol == EndpointProtocol.Https);

        return endpoints.Select(endpoint => new ServiceInstanceListener(serviceContext =>
            new KestrelCommunicationListener(serviceContext, endpoint.Name, (url, listener) =>
            {
                ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                return new WebHostBuilder()
                    .UseKestrel(options =>
                    {
                        if (endpoint.Protocol == EndpointProtocol.Http)
                            options.Listen(IPAddress.Any, endpoint.Port);
                        else if (endpoint.Protocol == EndpointProtocol.Https)
                            options.Listen(IPAddress.Any, endpoint.Port,
                                listenOptions => listenOptions.UseHttps(Certificates.GetCertificateFromLocalStore(
                                        Environment.GetEnvironmentVariable("ClusterCertifThumbprint"))));
                    })
                    .ConfigureServices(
                        services =>
                        {
                            services
                                .AddSingleton(new ConfigSettings())
                                .AddSingleton(serviceContext)
                                .AddSingleton(new HttpClient())
                                .AddSingleton(new FabricClient());
                        })
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseStartup<Startup>()
                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                    .UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))
                    .UseUrls(url)
                    .UseApplicationInsights()
                    .Build();
            }), endpoint.Name));
    }
}

}

1 个答案:

答案 0 :(得分:1)

您必须公开Facade类并删除sealed关键字(因为autofac创建了一个从您的类继承的代理类。如果被密封,则被禁止)。

所以改变

internal sealed class Facade : StatelessService

public class Facade : StatelessService

那么您就不再需要此了

[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]