且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用(在其中一种或多种)Ninject约定延长而不引用大会

更新时间:2022-11-25 23:19:32

这个问题是answerd在邮件列表上。 http://groups.google.com/group/ninject/browse_thread/thread/a7f2163e060a6d64

在短:

  1. 在表格(路径)需要一个路径或者相对于从工作目录或绝对
  2. 的组件必须,因为它被加载到加载上下文,以避免其它问题切换加载上下文驻留在探测路径。
  3. 在开发服务器使所有复杂的,因为它会将所有组件到自己的目录中,这使得它无法使用调用组件创建路径。这意味着使用开发服务器Web应用程序的唯一途径。
  4. 我们将增加对组件的完全限定的名字在未来的版本,以使它更容易些。

Sorry in advance for the long question, it's long because I've been digging at this all day.

The general problem:

I have an ASP.Net MVC2 application with the following projects: MyApp.Web, MyApp.Services, MyApp.Data.

We code to interfaces and utilize Ninject 2 for DI/IoC.

However, I'm getting awfully tired of typing (and forgetting to type):

Bind<ISomeService>.To<SomeService>;

So, knowing about Ninject.Extensions.Convensions, I have attempted to use it to automatically scan and register modules and simple dependencies of the type IXxxx => Xxxx.

What I tried that works (but isn't quite enough):

I can use the following code to setup Ninject, and everything seems to get wired up as expected.

    public static IKernel Initialize()
    {
        var kernel = new StandardKernel();

        kernel.Scan(a => {
                        a.FromAssemblyContaining<MyApp.Data.SomeDataClass>();
                        a.FromAssemblyContaining<MyApp.Services.SomeServiceClass>();
                        a.AutoLoadModules();
                        a.BindWithDefaultConventions();
                        a.InTransientScope();
                    });

        return kernel;
    }

What I want to accomplish instead:

However ... I'd like to take this a bit further in a way I think is supported, but I cannot seem to get it working.

Since our MyApp.Web project uses nothing at all (directly) from MyApp.Data, I am trying to avoid a reference to MyApp.Data. With the above code, I must reference MyApp.Data from MyApp.Web because of the compile time reference to SomeDataClass.

I would prefer to specify the name of an assembly for Ninject to scan and register. It seems the Conventions extension supports this through the From overloads that take a string (or enumerable of strings).

What I tried and how it breaks:

So, I've tried several variations on the From overloads:

    public static IKernel Initialize()
    {
        var kernel = new StandardKernel();

        kernel.Scan(a => {
                        a.From("MyApp.Data");
                        a.From("MyApp.Services.dll");
                        a.From("AnotherDependency, Version=1.0.0.0, PublicKeyToken=null"); //etc., etc. with the From(...)'s
                        a.AutoLoadModules();
                        a.BindWithDefaultConventions();
                        a.InTransientScope();
                    });

        return kernel;
    }

But I receive FileNotFoundExceptions with a message like:

Could not load file or assembly 'file:///C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\MyApp.Data' or one of its dependencies. The system cannot find the file specified.":"file:///C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\

What I've found trying to solve this myself:

I've checked out the source for Ninject.Extensions.Conventions, and I'll admit I get totally lost as to how this is supposed to work, but I can see what it is doing.

The assembly scanner builds up a list of assemblies to scan as we call various FromXXX methods.

When I call the From("assemblyName") method, it first checks if the list already contains any assemblies where the assembly.AssemblyName.Name equals the name I passed in (and AssemblyName.Name is the simple name, i.e. MyApp.Data, according to MSDN).

Flow passes through a couple unimportant methods, landing in the FindAssemblies method. This method takes the name I passed in (which we already saw is supposed to be a simple assembly name). It then creates a new AssemblyName, with our passed in name used as the AssemblyName.CodeBase.

Then, it attempts to Load the assembly into a temporary AppDomain. This is the step that fails with the above exception.

Obviously, the path it's searching is wrong, but I cannot supply a path through the From() method. That doesn't work either.

I've tried some of the other FromXXX methods, but I've gotten nowhere and spent too much time on this already. The FromAssembliesInPath and FromAssembliesMatching also do not work because, again, it is searching in the completely wrong directory.

Uh .. what was the question again:

Can anyone explain how to get Ninject Conventions to load assemblies by name, without creating a reference to the assembly and loading it by specifying a contained type? Please.

I've already searched through pages and pages of the Ninject google group, and I've read it's only (so it seems) relevant documentation and have not been able to solve it .. yet.

This question was answerd on the mailing list. http://groups.google.com/group/ninject/browse_thread/thread/a7f2163e060a6d64

In Short:

  1. Form(path) takes a path either relative from the working directory or absolute
  2. The assembly must reside in a probing path as it is loaded into the load context to avoid other problems with switching the loading context.
  3. The development server makes all complicated as it copies all assemblies into their own directory, which makes it impossible to use the calling assembly to create the path. This means the only way for web apps using the development server.
  4. We will add support for full qualified names of assemblies in a future version to make this easier.