.NET Core - brak zarejestrowanego serwisu

0

Korzystam z pewnego kursu pokazującego jak tworzyć mikroserwisy. Przy próbie uruchomienia kodu dostaję taki komunikat (pierwsze linijki to warningi dot. niekompatybilności pakietów):

C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities>dotnet run --urls "http://*:5050"
C:\Users\Marcin\source\repos\Actio\src\Actio.Common\Actio.Common.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project. [C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Actio.Services.Activities.csproj]
C:\Users\Marcin\source\repos\Actio\src\Actio.Common\Actio.Common.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.WebHost 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project. [C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Actio.Services.Activities.csproj]
C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Actio.Services.Activities.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Actio.Services.Activities.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.WebHost 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Actio.Services.Activities.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Actio.Services.Activities.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.WebHost 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
C:\Users\Marcin\source\repos\Actio\src\Actio.Common\Actio.Common.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
C:\Users\Marcin\source\repos\Actio\src\Actio.Common\Actio.Common.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.WebHost 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
Using launch settings from C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Properties\launchSettings.json...

Unhandled Exception: System.InvalidOperationException: Cannot resolve scoped service 'Actio.Common.Commands.ICommandHandler`1[Actio.Common.Commands.CreateActivity]' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Actio.Common.Services.ServiceHost.BusBuilder.SubscribeToCommand[TCommand]() in C:\Users\Marcin\source\repos\Actio\src\Actio.Common\Services\ServiceHost.cs:line 79
   at Actio.Services.Activities.Program.Main(String[] args) in C:\Users\Marcin\source\repos\Actio\src\Actio.Services.Activities\Program.cs:line 10

Wygląda na to, jakby źle był zarejestrowany serwis lub jakby brakowało mi jakiejś paczki. Problem w tym, że robię praktycznie wszystko wg poradnika. Kod wygląda następująco (kolejno wg wywołanych linijek). Prosiłbym o pomoc.

namespace Actio.Services.Identity
{
    public class Program
    {
        public static void Main(string[] args)
        {
            ServiceHost.Create<Startup>(args) // linia 10
                .UserRabbitMq()
                .SubscribeToCommand<CreateUser>()
                .Build()
                .Run();
        }
    }
}
            public BusBuilder SubscribeToCommand<TCommand>() where TCommand : ICommand
            {
                var handler = (ICommandHandler<TCommand>) _webHost.Services // linia 79
                    .GetService(typeof(ICommandHandler<TCommand>));
                _bus.WithCommandHandlerAsync(handler);

                return this;
            }
namespace Actio.Common.Commands
{
    public class CreateActivity : IAuthenticatedCommand
    {
        public Guid Id { get; set; }

        public Guid UserId { get; set; }

        public string Category { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public DateTime CreatedAt { get; set; }
    }
}
namespace Actio.Common.Commands
{
    public interface IAuthenticatedCommand : ICommand
    {
        Guid UserId { get; set; }
    }
}
namespace Actio.Common.Commands
{
    /// <summary>
    /// Marker interface
    /// </summary>
    public interface ICommand
    {
    }
}
0

refresh

0

Nie widać, żebyś rejestrował jakiekolwiek serwisy. Musisz je zarejestrować w ConfigureServices

0

Klasa ta wygląda tak i wszystko powinno być zarejestrowane:

namespace Actio.Services.Activities
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddRabbitMq(Configuration);
            services.AddScoped<ICommandHandler<CreateActivity>, CreateActivityHandler>();
        }
[...]

W pierwszym poście jako pierwsze okienko kodu powinienem jeszcze wrzucić:

namespace Actio.Services.Activities
{
    public class Program
    {
        public static void Main(string[] args)
        {
            ServiceHost.Create<Startup>(args)
                .UserRabbitMq()
                .SubscribeToCommand<CreateActivity>()
                .Build()
                .Run();
        }
    }
}

W miejsce namespace z Identity.

0

Zaczął bym od odinstalowania wszystkich niekompatybilnych nugetów:

Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.

I parę poniższych linijek

1 użytkowników online, w tym zalogowanych: 0, gości: 1