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

Doesn't work with .net core 2.0 #39

Open
trampster opened this issue Nov 12, 2017 · 10 comments
Open

Doesn't work with .net core 2.0 #39

trampster opened this issue Nov 12, 2017 · 10 comments

Comments

@trampster
Copy link

trampster commented Nov 12, 2017

I have:

  • Ninject 3.3.3
  • Ninject.Extensions.Factory 3.3.2

I have converted a working project from .net 4.6.1 to .net standard 2.0 and .net core 2.0. Now none of my Ninject.Extensions.Factory stuff works.

The Func<> automatic bindings are not happening, I get a runtime error saying it couldnt' find a binding for Func (I have worked around this be automatically binding all the Func<T's> that I need.

The interface factories like the following:
Bind<IWorkerThreadFactory>().ToFactory();

Result in a runtime error as follows:

System.NotImplementedException: 'This is a DynamicProxy2 error: There are no interceptors specified for method 'Tait.EnableFleet.Agent.FlowControl.IWorkerThread CreateWorkerThread(System.String)' which has no target. When calling method without target there is no implementation to 'proceed' to and it is the responsibility of the interceptor to mimic the implementation (set return value, out arguments etc)'

I have triple checked that my projects are using Ninject.Extensions.Factory 3.3.2 via nuget.

@scott-xu
Copy link
Member

Do you mean your UT doesn’t work? Or the deployed App doesn’t work?

@scott-xu
Copy link
Member

If it’s UT only, try workaround it by manually loading the factory module.

@trampster
Copy link
Author

I don't know what you mean by UT.

However I did discover that if I add new FuncModule() to my Kernel then it works. This is described here:

https://github.com/ninject/Ninject.Extensions.Factory/wiki/Manually-Loading-the-Factory-Extension-into-the-Ninject-Kernel

It doesn't mention this as being necessary for .net Core only for Silverlight. However it does fix my issue.

@scott-xu
Copy link
Member

I mean unit test

@trampster
Copy link
Author

OK, no not unit test, we don't use Ninject for our unit tests, We use Ninject only the composition root of our app. Unit testing is accomplished by constructor injecting mocks manually.

This is a .net core console app, running from visual studio.

@aerotog
Copy link

aerotog commented Jan 22, 2018

My team is also running into this problem using Ninject 3.3.4 and Ninject.Extensions.Factory 3.3.2 with .Net Core 2.0.

Interestingly, we only need to load FuncModule() when running on Windows. If we run the same application in Linux, it fails due to duplicate modules being loaded. To make it work on both, you can do something like:

var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

kernel = isWindows
    ? new StandardKernel(new RoutingHeartbeatModule(), new FuncModule())
    : new StandardKernel(new RoutingHeartbeatModule());

@aerotog
Copy link

aerotog commented Jan 23, 2018

I think I understand what's happening now. When building in VS or using "dotnet build", the resulting build does not include dependencies. Instead, the Core runtime relies on the Nuget cache to load them. Ninject probably looks for modules in the local directory to load dynamically which is why you have to manually load the Factory module.

When using "dotnet publish", the build directory has a stand alone version of the application with all dependencies in the local directory. This allows Ninject to discover and load the module dynamically.

I'm using the following which seems to be working in all cases:

kernel = new StandardKernel(new RoutingHeartbeatModule());

if (!kernel.HasModule(new FuncModule().Name))
{
    kernel = new StandardKernel(new RoutingHeartbeatModule(), new FuncModule());
 }

If this were added to documentation somewhere, this issue can probably be closed as I think it is working as intended.

@ryno1234
Copy link

ryno1234 commented Feb 5, 2019

While I can appreciate the insight @aerotog provided, this change in how .Net Core copies build output (or lack thereof) breaks the auto-discovery of Ninject extensions as well. Performing a "publish" fixes that issue, but creates a new one by removing the ability to easily run and debug the application using the Visual Studio interface. "Publish" is a workaround, but I'm curious if the Ninject team has a solution to the extension loading dilemma presented here.

@scott-xu
Copy link
Member

scott-xu commented Feb 5, 2019

try

<PropertyGroup>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

@ryno1234
Copy link

ryno1234 commented Feb 5, 2019

Thanks @scott-xu, that did the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants