azure - 服务结构 asp .net core IoC 错误 : cannot be dynamically proxied

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

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

The type Inovatic.SF.Windows.Facade.Facade cannot be dynamically proxied. Service types must not be sealed and need to be visible to the DynamicProxyGenAssembly2 assembly. This can be achieved by making the type public or adding the InternalsVisibleToAttribute to the assembly containing the type. e.g. [assembly: 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 类标记为公共(public),但这似乎不起作用

编辑:

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));
    }
}

}

最佳答案

您必须将Facade类公开并删除sealed关键字(因为autofac创建一个继承自您的类的代理类。如果它是密封的,那就是禁止)。

所以改变

internal sealed class Facade : StatelessService

public class Facade : StatelessService

那么你就不再需要这个了

[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]

关于azure - 服务结构 asp .net core IoC 错误 : cannot be dynamically proxied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514838/

相关文章:

asp.net - 具有 ASP.NET 成员身份的 Windows Azure 访问控制

java - Azure 上带有命令行参数的 Spring Boot 应用程序

c# - 如何在 Razor Pages 中实现两个具有单独 BindProperties 的表单?

c# - 如何在Asp.net core 2.0中使用log4net

azure - 如何在适用于 Linux 的 azure web 应用程序中自定义默认错误页面?

azure - 将 Blob 上传到 azure 返回 url

c# - 从 Core 2.0 中的类库访问 appsettings.json 中的设置

asp.net-mvc - 使用 Autofac 的 IoC

singleton - Autofac SingleInstance无法正常工作