8000 Use of unassigned local variable 'overILoggerM06D12di1' · Issue #99 · DevTeam/Pure.DI · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use of unassigned local variable 'overILoggerM06D12di1' #99

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

Open
ekalchev opened this issue Jun 12, 2025 · 2 comments
Open

Use of unassigned local variable 'overILoggerM06D12di1' #99

ekalchev opened this issue Jun 12, 2025 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@ekalchev
Copy link
ekalchev commented Jun 12, 2025

I am getting this error attempting to use ctx.Override() Use of unassigned local variable 'overILoggerM06D12di1'

@ekalchev
Copy link
Author
ekalchev commented Jun 12, 2025

I was able to reproduce the issue with minimal code

using Pure.DI;
using System.Diagnostics;

var composition = new Composition();
var service = composition.Service;

var serviceLoggerName = service.Logger.Name;
var repositoryLoggerName = service.Repository.Logger.Name;
Debug.Assert(serviceLoggerName == repositoryLoggerName, "Service and Repository should share the same logger instance.");

var proxyService = composition.ProxyService;
var proxyServiceLoggerName = proxyService.Service.Logger.Name;
repositoryLoggerName = proxyService.Service.Repository.Logger.Name;
Debug.Assert(proxyServiceLoggerName == proxyServiceLoggerName);

Console.ReadLine();

partial class Composition
{
	private void Setup() => DI.Setup("Composition")
		.Bind<Service>().To<Service>()
		.Bind<ILogger>().As(Lifetime.Singleton).To<Logger>(ctx => new Logger() { Name = "Default" })
		.Bind<ILogger>("TaggedLogger").As(Lifetime.Singleton).To(ctx => new Logger() { Name = "SpecialServiceLogger" })
		.Bind<Func<Repository>>().To<Func<Repository>>(ctx =>
		{
			ctx.Inject<Repository>(out var repo);
			return () => repo;
		})
		.Bind<IService>().To(ctx =>
		 {
			 ctx.Inject<ILogger>("TaggedLogger", out var logger);
			 ctx.Override(logger);
			 ctx.Inject<Service>(out var service);
			 return service;
		 })
		.Bind<IProxyService>().To<Proxy>()
		.Bind().To<Repository>()
		// Composition root
		.Root<IService>("Service")
		.Root<IProxyService>("ProxyService");
}

interface ILogger
{
	string Name { get; init; }
	void Log(string message);
}

class Logger : ILogger
{
	public required string Name { get; init; }
	public void Log(string message) { }
}

interface IRepository
{
	ILogger Logger { get; }
}

class Repository : IRepository
{
	public Repository(ILogger logger)
	{
		Logger = logger;
	}

	public ILogger Logger { get; }
}

interface IService
{
	ILogger Logger { get; }
	IRepository Repository { get; }
}

class Service : IService
{
	private readonly Func<IRepository> repositoryFactory;

	public Service(ILogger logger, Func<IRepository> repositoryFactory)
	{
		Logger = logger;
		this.repositoryFactory = repositoryFactory;
	}

	public IRepository Repository => repositoryFactory();

	public ILogger Logger { get; }
}

interface IProxyService
{
	IService Service { get; }
}

class Proxy : IProxyService
{
	public Proxy(IService service)
	{
		Service = service;
	}

	public IService Service { get; }
}

@NikolayPianikov
Copy link
Member

@ekalchev thank you for reporting

@NikolayPianikov NikolayPianikov added the bug Something isn't working label Jun 14, 2025
@NikolayPianikov NikolayPianikov self-assigned this Jun 14, 2025
@NikolayPianikov NikolayPianikov added this to the v 2.2.0 milestone Jun 14, 2025
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
0