Error thrown on method Import with key
See original GitHub issueThe 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:
- Created a year ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.InjectionContextValueProvider.GetValueFromInjectionContext<T>(...)
IExtraDataContainer.GetExtraData(key);
not returning object, butIExportLocatorScope.locate<T>(withKey: key);
will. Looks like IExtraDataContainer is getting cast incorrectly from IExportLocatorScope.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