即使所有对象都可用,也无法使用AutoFac解决Azure Service Fabric服务

时间:2018-08-04 16:55:41

标签: c# autofac azure-service-fabric

我正在尝试使用Autofac进行注入:

Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures during open on _Node_0. API call: IStatelessServiceInstance.Open(); Error = Autofac.Core.DependencyResolutionException (-2146233088)
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ServiceFabricProxy (DelegateActivator), Services = [MYSERVICE.ServiceFabric.ServiceFabric], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned ---> An error occurred during the activation of a particular registration. See the inner exception for 
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Integration.ServiceFabric.StatelessServiceFactoryRegistration.<>c__DisplayClass0_0`1.b__0(StatelessServiceContext context)
   at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceFactory.System.Fabric.IStatelessServiceFactory.CreateInstance(String serviceTypeName, Uri serviceName, Byte[] initializationData, Guid partitionId, Int64 instanceId)
   at System.Fabric.ServiceFactoryBroker.<>c__DisplayClass9_0.b__0(IStatelessServiceFactory factory, ServiceInitializationParameters initParams)
   at System.Fabric.ServiceFactoryBroker.CreateHelper[TFactory,TReturnValue](IntPtr nativeServiceType, IntPtr nativeServiceName, UInt32 initializationDataLength, IntPtr nativeInitializationData, Guid partitionId, Func`3 creationFunc, Action`2 initializationFunc, ServiceInitializationParameters initializationParameters)
For more information see: http://aka.ms/sfhealth

这些错误总是令人沮丧,因为它们不会停止Visual Studio!

public ServiceFabric(StatelessServiceContext context, ServiceLoggerFactory serviceLoggerFactory, ServiceConfiguration configuration)
    : base(context)
{
    Argument.IsNotNull(() => context);
    Argument.IsNotNull(() => serviceLoggerFactory);
    Argument.IsNotNull(() => configuration);

    _log = serviceLoggerFactory.EnrichLoggerForStatelessServiceContext(this);
    _log.Information($"Service constructed {GetType().FullName}");
}

我已验证构造函数中的类已注册:

public class NodeAutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var assembly = typeof(GlobalAutofacModule).Assembly;
        var appName = assembly.GetName().Name;
        var appVersion = assembly.InformationalVersion();

        builder.Register<ILogger>(container => LoggingHelper.ConfigureLogging(appName, appVersion))
            .SingleInstance();

        builder.RegisterType<ServiceLoggerFactory>()
            .SingleInstance();

        builder.RegisterType<ServiceConfiguration>()
            .AsSelf()
            .SingleInstance();

        // TODO: Register services here
    }
}

当AutoFac无法解析构造函数时,我发现使用它真的很沮丧。

发生这种情况时,有什么方法可以获取更多信息吗?

我的Service Fabric服务启动时正在创建容器。

保罗

1 个答案:

答案 0 :(得分:1)

在SF错误中剪切stacktrace确实很烦人。但是有一种获取错误的更多详细信息的方法。配置容器后,请尝试以下操作:

import sbt._
import Keys._

object TestExtraShotPlugin extends AutoPlugin {
  override def requires = plugins.JvmPlugin
  override def trigger = allRequirements
  object autoImport {
    val testExtraShot = taskKey[Unit]("test then testQuick only on failure")
  }
  import autoImport._

  override def projectSettings = {
    testExtraShot := (Def.taskDyn {
      val t = (Test / test).result.value
      if (t.toEither.isLeft) (Test / testQuick).toTask("")
      else
        Def.task {
          val s = streams.value
          s.log.info("ok!")
        }
    }).value
  }
}

因此,您将可以访问完整的“例外”信息,并可以指定问题。