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
Cannot inject a struct using a Func wrapper #109
Comments
Thanks for reporting, Anyway, I am planning to fix the problem on DryIoc side. |
Thanks a lot @dadhi for you time and patience. I have done some research of my own in the meantime, arriving to the same conclusion: DryIoc generates a lambda that tries to return an unboxed struct in lieu of an This also explains the I also thank you for the workaround, although I have always used I probably should have mentioned earlier that the application that's giving me problems runs under latest Mono on a Raspberry Pi. 😀 The exception thrown when using FastExceptionCompiler is different:
It seems to me that Mono throws a different exception, but for exactly the same reason: the fact that the RET is considered invalid kind of gives it away. What's strange is that the exception occurs even when using |
DryIoc v4.0.3 with the fix is out |
Thanks a lot @dadhi! |
The situation
Using DryIoc v4.0.2:
A class implementing a service takes a
CancellationToken
as a constructor parameter.I register a singleton
CancellationToken
in the container.I do not resolve the class directly, but use the
Func<>
wrapper to obtain a delegate returning an instance of the service.Expected outcome
I would expect to be able to call the resolved delegate, obtaining an instance of the class implementing the service.
Actual outcome
When using FastExpressionCompiler (the default), DryIoc can resolve the delegate, but the resolved delegate throws a
System.InvalidCastException
with the following message:When trying the same code in a .NET fiddle the exception thrown is different, maybe due to some sort of sandboxing on the server:
When NOT using FastExpressionCompiler, DryIoc cannot even resolve the delegate. Instead a
System.ArgumentException
is thrown with the following message:Things I've tried
Explicitly specifying Singleton reuse for the cancellation token vs. having a default Reuse of Singleton: no change.
Registering the service as transient vs. singleton: no change.
Registering both
CancellationTokenSource
andCancellationToken
as singletons (with all the requiredMade
trickery) vs. having an externally createdCancellationTokenSource
and usingRegisterDelegate(_ => cts.Token, reuse: Reuse.Singleton)
: no change.A PR with relevant unit tests in on its way.
The text was updated successfully, but these errors were encountered: