lucabriguglia / OpenCQRS

.NET Standard framework to create simple and clean design. Advanced features for DDD, CQRS and Event Sourcing.
Apache License 2.0
3 stars 115 forks source link

InvalidOperationException: Unable to resolve service for type #102

Closed xiongdashan closed 4 years ago

xiongdashan commented 4 years ago

.net core 3.1 mvc api

add Kledex in startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddKledex(typeof(Program));
            services.AddControllers();
        }

exception: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Builder.IApplicationBuilder' while attempting to activate 'Kledex.Extensions.KledexAppBuilder'.

lucabriguglia commented 4 years ago

I'll try to reproduce it

lucabriguglia commented 4 years ago

It works if you change Program.cs from:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

to:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

It looks like the IWebHostBuilder doesn't register the IApplicationBuilder

xiongdashan commented 4 years ago

Okay. So this framework can't use in .net framework 4.8?

lucabriguglia commented 4 years ago

Kledex is based on .NET Standard 2.1 which is not currently supported in .NET Framework 4.8. Please check the compatibility for .NET Standard here: https://docs.microsoft.com/en-us/dotnet/standard/net-standard.

xiongdashan commented 4 years ago

How can i use Autofac directly, without ServiceCollection;

Like

  var servcies = new ServiceCollection();
            servcies.AddKledex();

to

  var builder = new ContainerBuilder()
  builder.Register......
xiongdashan commented 4 years ago
Severity    Code    Description Project File    Line    Suppression State
Error       Could not install package 'Kledex 2.5.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.8', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.             
lucabriguglia commented 4 years ago

You should be able to do something like this:

servcies.AddKledex();
var builder = new ContainerBuilder();
builder.Populate(services);
// add other stuff to Autofac
xiongdashan commented 4 years ago

Yes, I using builder.Populate... But my project is Mvc Web Api using .net framewrok 4.8.

lucabriguglia commented 4 years ago

As I have already mentioned .NET Standard 2.1 is not supported yet in .NET Framework 4.8.

xiongdashan commented 4 years ago

Okay, thanks for your replay。

lucabriguglia commented 4 years ago

Closing as it is established that it's a compatibility issue.

Vikaskumargd commented 4 years ago

Do we have a permanent fix for this without changing the Program.cs ? not below one :

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}
lucabriguglia commented 4 years ago

I don't think so, sorry.