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

AutoConcreteTypeResolution should not consider a primitive type #16

Closed
jkvargas opened this issue Sep 12, 2018 · 6 comments
Closed

AutoConcreteTypeResolution should not consider a primitive type #16

jkvargas opened this issue Sep 12, 2018 · 6 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@jkvargas
Copy link

jkvargas commented Sep 12, 2018

Follows the error in the comment,
I suppose that I must register something regarding that function: Func<String, Microsoft.Extensions.Logging.LogLevel, Boolean>. Can you guys provide me some direction? =)

Btw, I am using DryIoc 3.0.2 and DryIoc.Microsoft.DependencyInjection 2.1.0.

//      DryIoc.ContainerException: 'Unable to get constructor of Boolean using provided constructor selector when resolving Boolean {ReturnDefault} #571 with args [_String0]
  //in wrapper Func<String, Microsoft.Extensions.Logging.LogLevel, Boolean> { ReturnDefault} as parameter "filter" #11 with args [_String0]
  //in singleton Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider: Microsoft.Extensions.Logging.ILoggerProvider { ServiceKey = DefaultKey(0), ReturnDefault} #56
  //in wrapper IEnumerable<Microsoft.Extensions.Logging.ILoggerProvider> { ReturnDefault} as parameter "providers" #1
  //in singleton Microsoft.Extensions.Logging.LoggerFactory: Microsoft.Extensions.Logging.ILoggerFactory as parameter "factory" #42
  //in singleton Microsoft.Extensions.Logging.Logger<Microsoft.AspNetCore.Hosting.Internal.WebHost>: Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Hosting.Internal.WebHost> #570
  //from container.

// Startup.cs

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
 
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySql(
                    Configuration.GetConnectionString("DataBaseIntranetCore")));
            services.AddDefaultIdentity<IdentityUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>();
 
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 
            return services.ConfigureDI(Bootstrap.RegisterServices);
        }

// IocDiExtensions.cs

public static class IocDiExtensions
    {
        public static IServiceProvider ConfigureDI(this IServiceCollection serviceCollection, Action<IRegistrator> configureServices)
        {
            var container = new Container(rules => rules
                .WithoutThrowOnRegisteringDisposableTransient()
                .WithTrackingDisposableTransients()
                .WithAutoConcreteTypeResolution())
                .WithDependencyInjectionAdapter(serviceCollection);
 
            container.UseInstance(container);
 
            configureServices(container);
 
            return container.Resolve<IServiceProvider>();
        }
    }

// Bootstrap

public static void RegisterServices(IRegistrator registrator)
{
// ILogger here is something I made, is not the same from Microsoft.Extensions.Logging.ILogger

            registrator.Register<ILogger, LogWrapper>(Reuse.Transient);
            registrator.Register<IAppConfig, AppConfig>(Reuse.Transient);
 
            registrator.Register<ICoreDbContext>(made: Made.Of(() => new CoreDbContext()));
            registrator.Register<ICoreDbContext>(made: Made.Of(() => new CoreDbContext("DataBaseCoreRO")), serviceKey: "ReadOnly");
            registrator.Register<IContextFactory, ContextFactory>();

/// etc...
}
@dadhi
Copy link
Owner

dadhi commented Sep 12, 2018

Func<String, Microsoft.Extensions.Logging.LogLevel, Boolean>

The func returns bool, so it should be registered.

How and what does it mean is up to you.
You may register a method returning bool (via Made.Of) and use service key to identify it.

@jkvargas
Copy link
Author

jkvargas commented Sep 15, 2018

dadhi, I believe I still need your help. I am running a asp.net core app. I tryed to follow examples around the internet but I am stuck.

Btw, I created a bare netcore 2.1 project if you have a moment to take a look.
https://github.com/jkvargas/samplemvcdryioc

I did try to minimize the code of my app, I just created a new mvc app with individual authentication using netcoreapp2.0. And I've tryed to integrate DryIoc.

`public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var container = new Container(rules => rules
                .WithoutThrowOnRegisteringDisposableTransient()
                .WithTrackingDisposableTransients()
                .WithAutoConcreteTypeResolution());

            container.Register<IAppConfig, AppConfig>(Reuse.Transient);

            var newContainer = container.WithDependencyInjectionAdapter(services, throwIfUnresolved: type => type.Name.EndsWith("Controller"));

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            return newContainer.Resolve<IServiceProvider>();
        }`

It keeps returning "DryIoc.ContainerException: 'Unable to get constructor of Boolean using provided constructor selector when resolving Boolean {ReturnDefault} #101 with args [_String0]
in wrapper Func<String, Microsoft.Extensions.Logging.LogLevel, Boolean> {ReturnDefault} as parameter "filter" #11 with args [_String0]
in singleton Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider: Microsoft.Extensions.Logging.ILoggerProvider {ServiceKey=DefaultKey(0), ReturnDefault} #51
in wrapper IEnumerable<Microsoft.Extensions.Logging.ILoggerProvider> {ReturnDefault} as parameter "providers" #1
in singleton Microsoft.Extensions.Logging.LoggerFactory: Microsoft.Extensions.Logging.ILoggerFactory as parameter "factory" #41
in singleton Microsoft.Extensions.Logging.Logger<Microsoft.AspNetCore.Hosting.Internal.WebHost>: Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Hosting.Internal.WebHost> #100
from container.'"

Isn't this a problem with the integration?

@dadhi
Copy link
Owner

dadhi commented Sep 15, 2018

Now looking at used setup and exception I have more clues, thanks.

What if you remove WithAutoConcreteTypeResolution() ?

Any reason you need it?

Looking at your example, I have an idea how to improve exception messsge when using Auto resolution.

When I have time, I will check more thoroughly.

@jkvargas
Copy link
Author

Yeah, it seems that was the problem.
I can handle without WithAutoConcreteTypeResolution =)
Thanks man

@dadhi
Copy link
Owner

dadhi commented Sep 16, 2018

Cool, anyway need to improve some things.

AutoConcreteTypeResolution probably should not consider Func arguments.

@dadhi
Copy link
Owner

dadhi commented Sep 24, 2018

After some thoughts, the automatic resolution should not consider a primitive type, like int, but using a Func<MyType> actually is fine.

@dadhi dadhi changed the title Missing something? AutoConcreteTypeResolution should not consider a primitive type Sep 24, 2018
dadhi added a commit that referenced this issue Sep 24, 2018
…#16

fixed: Test in docs by replacing log4net with Nlog example
@dadhi dadhi closed this as completed Sep 24, 2018
@dadhi dadhi self-assigned this Sep 24, 2018
@dadhi dadhi added the bug Something isn't working label Sep 24, 2018
@dadhi dadhi added this to the 3.1.0 milestone Sep 24, 2018
@dadhi dadhi modified the milestones: 3.1.0, 4.0.0 Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants