Search code examples
c#autofacazure-service-fabric

Cannot resolve Azure Service Fabric service with AutoFac even all objects are available


I am trying to inject using 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

These errors are always frustrating as they don't stop 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}");
}

I have verified that the classes in my constructor have been registered:

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

I find AutoFac really frustrating to work with when it can't resolve a constructor.

Are there any ways to get more information when this happens?

I am creating my container when my Service Fabric service starts up.

Paul


Solution

  • Cut stacktrace in SF errors is really annoying. But there is a way to get more details of the error. Try the following after configuration of your container:

    using (var c = builder.Build())
    {
        var s = c.Resolve<ServiceFabric>();
    }
    

    Thus you'll get access to the full Exception information and will be able to specify the problem.