Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service is not register in Eureka because of Autofac #1186

Closed
xXAvoraXx opened this issue Sep 27, 2023 · 10 comments
Closed

Service is not register in Eureka because of Autofac #1186

xXAvoraXx opened this issue Sep 27, 2023 · 10 comments
Assignees
Labels
Component/Discovery Issues related to Steeltoe Service Discovery ReleaseLine/3.x Identified as a feature/fix for the 3.x release line ReleaseLine/4.x Identified as a feature/fix for the 4.x release line Type/bug Something isn't working

Comments

@xXAvoraXx
Copy link

Describe the bug

I am trying to register my .net 7 web api application to eureka server, but I am getting the following error, why?

Code Explain

I use Autofac for service registrations, I make autofac configurations in a separate dotnet 7 library project. Even though I wrote the necessary configurations to connect to the Eureka service, I get an error.

Codes: CoreModule

    using Project.Services.Core.Extensions;
    using Project.Services.Core.Utilities.IoC;
    using Microsoft.Extensions.DependencyInjection;
    using StackExchange.Redis;
    using System.Diagnostics;
    using Serilog;
    using Microsoft.Extensions.Configuration;
    using Steeltoe.Discovery.Client;
    using Steeltoe.Discovery.Eureka;
    
    namespace Project.Services.Core.DependencyResolvers
    {
        public class CoreModule : ICoreModule
        {
            public void Load(IServiceCollection services, IConfiguration configuration)
            {
                services.AddServiceDiscovery(o => o.UseEureka());
    
    
                services.AddMemoryCache();
                services.AddSingleton<ICacheManager, MemoryCacheManager>();
                services.AddSingleton<IDistributedCacheManager, DistributedCacheManager>();
                services.AddHttpContextAccessor();
                services.AddSingleton<Stopwatch>();
    
            }
        }
    }

Program.cs

    builder.Services.AddDependencyResolvers(new ICoreModule[]
                {
                    new CoreModule()
                }, builder.Configuration);

appsettings.json config

      "Spring": {
        "Application": {
          "Name": "STOCK-SERVICE"
        }
      },
      "Eureka": {
        "Client": {
          "ServiceUrl": "http://localhost:867/eureka/",
          "ValidateCertificates": false,
          "ShouldRegisterWithEureka": true
        },
    
        // No docke instance added
        "Instance": {
          //"port": 5004,
          "NonSecurePort": 5004,
          "HostName": "localhost",
          "InstanceId": "Stock.API,Port:5004",
          "StatusPageUrlPath": "/swagger/index.html"
          //"HealthCheckUrlPath": "/api/values/healthcheck"
        }
      }

Error Details

    Autofac.Core.DependencyResolutionException
      HResult=0x80131500
      Message=An exception was thrown while activating λ:Microsoft.Extensions.Hosting.IHostedService[] -> Steeltoe.Discovery.Client.DiscoveryClientService -> λ:Steeltoe.Discovery.IDiscoveryClient -> λ:Steeltoe.Discovery.Eureka.EurekaDiscoveryClient -> Steeltoe.Discovery.Eureka.EurekaApplicationInfoManager.
      Source=Autofac
      StackTrace:
       at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.RegistrationPipelineInvokeMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.SharingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.ResolveOperation.InvokePipeline(ResolveRequest request, DefaultResolveRequestContext requestContext)
       at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)
       at Autofac.Core.Resolving.ResolveOperation.ExecuteOperation(ResolveRequest request)
       at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request)
       at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request)
       at Autofac.Core.Container.ResolveComponent(ResolveRequest request)
       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(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
       at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType)
       at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
       at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__12.MoveNext()
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
       at Program.<Main>$(String[] args) 
    
    
    DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsMonitor`1[Steeltoe.Discovery.Eureka.EurekaInstanceOptions], Microsoft.Extensions.Logging.ILoggerFactory)' on type 'EurekaApplicationInfoManager'.

    NullReferenceException: Object reference not set to an instance of an object.

Steps to reproduce

Steps to reproduce the behavior:
1.
2.

Expected behavior

A clear and concise description of what you expected to happen.

Environment (please complete the following information):

  • Steeltoe Version: 3.2.4
  • Platform: [e.g. Cloud Foundry, Azure, etc.)
  • OS: Windows 11
  • .NET Version: 7
  • Any other library versions to note
  • Autofac: 6.4.0

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context or links

Add any other context about the problem here.

@xXAvoraXx xXAvoraXx added the Type/bug Something isn't working label Sep 27, 2023
@xXAvoraXx
Copy link
Author

@TimHess @hananiel

@TimHess
Copy link
Member

TimHess commented Sep 27, 2023

Hi @xXAvoraXx, any chance you can provide a minimal app that reproduces the issue? At a glance, this looks like either the LoggerFactory isn't registered with AutoFac or there is an issue with retrieving the options

@xXAvoraXx
Copy link
Author

xXAvoraXx commented Sep 27, 2023

Can you @TimHess check this repo -> https://github.com/xXAvoraXx/SteeltoeProject

I have prepared a small demo version for you to review.

@TimHess
Copy link
Member

TimHess commented Sep 27, 2023

@xXAvoraXx the project runs fine if you run it directly, it's only when you use docker compose that it fails. This output suggests the issue is that the Core project isn't included in the container:

1>#12 [build 4/7] RUN dotnet restore "SteeltoeProject.Services.API.WebAPI/SteeltoeProject.Services.API.WebAPI.csproj"
1>#12 1.246   Determining projects to restore...
1>#12 1.251   Skipping project "/src/src/Services/Common/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core.csproj" because it was not found.
1>#12 1.254   Skipping project "/src/src/Services/Common/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core.csproj" because it was not found.
1>#12 3.709   Restored /src/SteeltoeProject.Services.API.WebAPI/SteeltoeProject.Services.API.WebAPI.csproj (in 2.15 sec).
1>#12 DONE 3.8s

@xXAvoraXx
Copy link
Author

@xXAvoraXx the project runs fine if you run it directly, it's only when you use docker compose that it fails. This output suggests the issue is that the Core project isn't included in the container:

1>#12 [build 4/7] RUN dotnet restore "SteeltoeProject.Services.API.WebAPI/SteeltoeProject.Services.API.WebAPI.csproj"
1>#12 1.246   Determining projects to restore...
1>#12 1.251   Skipping project "/src/src/Services/Common/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core.csproj" because it was not found.
1>#12 1.254   Skipping project "/src/src/Services/Common/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core.csproj" because it was not found.
1>#12 3.709   Restored /src/SteeltoeProject.Services.API.WebAPI/SteeltoeProject.Services.API.WebAPI.csproj (in 2.15 sec).
1>#12 DONE 3.8s

How do I test it without running it in docker-compose? All our projects run on docker-compose, including prod.

@TimHess
Copy link
Member

TimHess commented Sep 27, 2023

You can use dotnet run from the context of the api project folder, or (at least for this demo app) use the http or https profiles defined in launchSettings.json. There should be a dropdown selector on the right side of the start button in Visual Studio that lets you switch between profiles

@xXAvoraXx
Copy link
Author

xXAvoraXx commented Sep 27, 2023

You can use dotnet run from the context of the api project folder, or (at least for this demo app) use the http or https profiles defined in launchSettings.json. There should be a dropdown selector on the right side of the start button in Visual Studio that lets you switch between profiles

Can you check the project again? I placed the "core" project in the Dockerfile.

COPY ["src/Services/Common/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core.csproj", "src/Services/Common/SteeltoeProject.Services.Core/SteeltoeProject.Services.Core/"]

@xXAvoraXx
Copy link
Author

The Autofac community was no help in this regard. I need your help. @TimHess

@TimHess TimHess added ReleaseLine/3.x Identified as a feature/fix for the 3.x release line Component/Discovery Issues related to Steeltoe Service Discovery ReleaseLine/4.x Identified as a feature/fix for the 4.x release line labels Sep 28, 2023
@TimHess TimHess self-assigned this Sep 28, 2023
@TimHess
Copy link
Member

TimHess commented Sep 28, 2023

Well, I've identified the issue. The fix is trivial and you can work around it by removing the trailing semicolon on this line

A NullReferenceException is thrown on this line because there is an "address" of null that comes from this code blindly adding everything from the split operation on the line above.

Thanks for the report! It's unfortunate the way this bug manifested, it really isn't clear why it all fails, but at least it's a simple fix.

@xXAvoraXx
Copy link
Author

I'm really surprised :) I've been trying for 2 days because of a simple comma, I never thought of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component/Discovery Issues related to Steeltoe Service Discovery ReleaseLine/3.x Identified as a feature/fix for the 3.x release line ReleaseLine/4.x Identified as a feature/fix for the 4.x release line Type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants