Dependency Injection not resolving when referencing another project.

Topics: Troubleshooting
Jun 5, 2013 at 5:04 AM
I know that I need to inherit any interface I wish to be injected, from IDependency.

If I create an interface within the same Module assembly it works.
However, when I move that interface to have it's own project, hence its own assembly, and add a reference to that interface assembly, it fails to inject. In my case, I got Test.Interface.dll.

Here's the error I got:
None of the constructors found with 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'Test.Controllers.SomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'Test.Interface.ISomeService someService' of constructor 'Void .ctor(Test.Interface.ISomeService)'.
Any help appreciated.
Jun 5, 2013 at 5:32 AM
When you say its own project, you mean its own module?
Jun 5, 2013 at 5:54 AM
Say, I have a MyModule.csproj. It has the following Structure, I will omit the rest:-

The controller injects this with no issues.

New Scenario:

I want to centralize all my common services into one project, call it Test.Interface.csproj. It contains:
ISomeService.cs (Of course, I add reference to Orchard.dll and derived from IDependency. This is the same file in MyModule.csproj)

I also added Test.Impl.csproj, that contains concrete classes as followed:
public class SomeService : ISomeService

Now, in my MyModule.csproj, I removed the two files (ISomeService.cs and SomeService.cs) and added the reference to Test.Interface.csproj (which is Test.Interface.dll).

Now, I essentially have MyModule.dll, Test.Interface.dll and Test.Impl.dll. I am guessing, because it's not in the module Bin\, at run-time it fail ?
Jun 5, 2013 at 6:11 AM
No, I mean a module in the Orchard sense, with a manifest, that lives under the Modules folder, etc.
Jun 5, 2013 at 6:19 AM
In that case No. How do you advice I go about doing this?
Jun 5, 2013 at 10:39 AM
Take a look at the Orchard.Blogs module. you need a module.txt file, this tells Orchard that this is a module (Bertrand referred to it as a manifest.)

Once you have done this, enable the module through the modules admin screen or via the command line.
Jun 5, 2013 at 7:36 PM
Edited Jun 5, 2013 at 7:36 PM
You need to
  • either put the Module.txt file in your library to make it a module, or
  • register services from external library manually by implementing an Autofac module (there are lot's of examples in the core code)
Orchard auto-registers only those classes coming from modules, not from all referenced libraries. It doesn't matter if you implement IDependency or not - it won't get registered unless your project is a module, ie. it contains a manifest file. In addition, like Nick said, you need to enable your module - dependencies from disabled modules/features are not registered either.
Jun 6, 2013 at 2:49 PM
Thank you all. Pszmyd answered my question. Key is something like LoggerModule.cs where you derived from AutoFac module. Once again, I appreciate everyone's help.