question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error thrown on method Import with key

See original GitHub issue

The fallowing is the coding I am using (note the different commented out elements to test behaviors are working properly in other ways).

// See https://aka.ms/new-console-template for more information

using Grace.DependencyInjection;
using Grace.DependencyInjection.Attributes;

DependencyInjectionContainer container = new DependencyInjectionContainer((c) => {
	c.AutoRegisterUnknown = true;
	c.Trace = (s => {
		Console.WriteLine(s);
	});
});

container.Configure((c) => {
	c.Export<Service>().AsKeyed<IService>("ServiceM");

	c.Export<ServiceAlt>().AsKeyed<IService>("ServiceA");

	// Will default to this if a key import fails.
	// Allows for the default [import()].
	//c.Export<Service>().As<IService>();
});

var scope = container.BeginLifetimeScope("RootScope");

var alt = container.Locate<IService>(withKey: "ServiceA");
var service = container.Locate<ServiceMain>();

Console.WriteLine(service.Service.GetString());

interface IService {
	string GetString();
}

class Service : IService {
	public virtual string GetString() {
		return "Service";
	}
}

class ServiceAlt : IService {
	public string GetString() {
		return "Service AlternativeService";
	}
}

class ServiceMain {
	// Works.
	//[Import(Key = "ServiceA")]
	public IService Service { get; set; }

	// Works with export "c.Export<Service>().As<IService>();"
	//[Import()]
	
	// Throws error that type reference in Grace is null.
	[Import(Key = "ServiceA")]
	public void Recieve(IService service) {
		Console.WriteLine("Import through method");
		Service = service;
	}
}

The above throws the fallowing error.

Unhandled exception. Grace.DependencyInjection.Exceptions.LocateException: Could not locate Type IService
1 Importing ServiceMain
2 Importing IService  for method Recieve parameter service

   at Grace.DependencyInjection.Impl.InjectionContextValueProvider.GetValueFromInjectionContext[T](IExportLocatorScope locator, StaticInjectionContext staticContext, Object key, IInjectionContext dataProvider, Object defaultValue, Boolean useDefault, Boolean isRequired)
   at lambda_method2(Closure , IExportLocatorScope , IDisposalScope , IInjectionContext )
   at Grace.DependencyInjection.Impl.ActivationStrategyDelegateCache.FallbackExecution(ImmutableHashTree`2 currentNode, Type type, IExportLocatorScope scope, Boolean allowNull, IInjectionContext context)
   at Grace.DependencyInjection.Impl.ActivationStrategyDelegateCache.ExecuteActivationStrategyDelegate(Type type, IExportLocatorScope scope)
   at Grace.DependencyInjection.Impl.BaseExportLocatorScope.Locate[T]()
   [My code url]/Program.cs:line 25

Grace version 8.0.0-Beta822

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
XanNavacommented, Nov 6, 2022

Still not sure why the key isn’t being passed in, but the call GetValueFromExtraDataProvider<T>(key, currentLocator, out value) seems to not be working properly and where the method call ends and value is still null if there is a key value. I have narrowed the issue down to seemingly the two fallowing things.

  1. Key not being passed into InjectionContextValueProvider.GetValueFromInjectionContext<T>(...)
  2. IExtraDataContainer.GetExtraData(key); not returning object, but IExportLocatorScope.locate<T>(withKey: key); will. Looks like IExtraDataContainer is getting cast incorrectly from IExportLocatorScope.
0reactions
ipjohnsoncommented, Nov 6, 2022

It looks like there was an issue in the auto configuration code that wasn’t checking attributes on the method parameters.

I’ve released an updated version 8.0.0-RC824.

Note you need to specify the import on the parameter as well as the method

  class ServiceMain
  {
      public IService Service { get; set; }
      
      [Import]
      public void Recieve([Import(Key = "ServiceA")]IService service)
      {
          Console.WriteLine("Import through method");
          Service = service;
      }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java importing Package wont work [Compiler throwing ...
Closed 8 years ago. When I try to import another package, it throws some weird error. I'm working with eclipse and imported the...
Read more >
Python KeyError Exceptions and How to Handle Them
In this tutorial, you'll learn how to handle Python KeyError exceptions. They are often caused by a bad key lookup in a dictionary,...
Read more >
How to Throw Exceptions in Java
In the example below, we have created a test method to demonstrate throwing an exception. The toString() method returns a textual representation ...
Read more >
How to Throw Exceptions in Python
Python Try Catch Exception Example ; import datetime current_date = datetime.now() print("Current date is: " ; input("Enter date in yyyy-mm-dd ...
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
Throwing a user-defined error. This example defines a function that throws a TypeError if the input is not of the expected type. js...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found