如何使用(在其中一种或多种)Ninject约定延长而不引用大会 [英] How to use Ninject Conventions extension without referencing Assembly (or Types within it)

查看:167
本文介绍了如何使用(在其中一种或多种)Ninject约定延长而不引用大会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起提前为长的问题,这是很长,因为我一直在挖这个一整天。

的普遍问题:

我有一个ASP.Net MVC2应用程序有以下项目:MyApp.Web,MyApp.Services,MyApp.Data

我们code到接口和利用Ninject 2 DI / IoC的。

不过,我收到的非常的疲惫打字(和忘记型):

 绑定< ISomeService>。为< SomeService取代;
 

所以,寂寂Ninject.Extensions.Convensions,我试图用它来自动扫描并注册模块和类型IXxxx => XXXX简单的相关性。

什么我试过作品(但不是很足够了):

我可以使用下面的code设置Ninject,一切似乎变得接线预期。

 公共静态的iKernel初始化()
    {
        VAR内核=新StandardKernel();

        kernel.Scan(一个=> {
                        a.FromAssemblyContaining&其中; MyApp.Data.SomeDataClass>();
                        a.FromAssemblyContaining&其中; MyApp.Services.SomeServiceClass>();
                        a.AutoLoadModules();
                        a.BindWithDefaultConventions();
                        a.InTransientScope();
                    });

        返回内核;
    }
 

我要完成的任务,而不是什么:

不过......我想远一点借此在某种程度上我的认为的支持,但我似乎无法得到它的工作。

由于我们的MyApp.Web项目使用从MyApp.Data什么都没有(直接),我试图避免提及MyApp.Data。有了上面code,我的必须的参考MyApp.Data从MyApp.Web编译时参考SomeDataClass因。

我会preFER指定的程序集的名称的为Ninject扫描和注册。看来约定扩展支持这种通过从它接受一个字符串(或枚举的字符串)。

过载

我试着和它是如何打破:

所以,我试过几个变化在从过载:

 公共静态的iKernel初始化()
    {
        VAR内核=新StandardKernel();

        kernel.Scan(一个=> {
                        a.From(MyApp.Data);
                        a.From(MyApp.Services.dll);
                        a.From(AnotherDependency,版本= 1.0.0.0,公钥=空); //等等,等等,与从(...)的
                        a.AutoLoadModules();
                        a.BindWithDefaultConventions();
                        a.InTransientScope();
                    });

        返回内核;
    }
 

不过,我收到FileNotFoundExceptions用类似这样的消息:

  

无法加载文件或程序集   文件:/// C:\ Program Files文件   (86)\共同文件\微软   共享\ DevServer \ 10.0 \ MyApp.Data   它的依赖或之一。系统   找不到文件   指定:文件:/// C:\ Program Files文件   (86)\共同文件\微软   共享\ DevServer \ 10.0 \

我发现什么试图解决这个自己:

我已经签出了源Ninject.Extensions.Conventions,我会承认,我得到完全失去了为这是如何工作的,但我可以看到它在做什么。

组装扫描仪建立了组件列表进行扫描,我们调用各种FromXXX的方法。

在我所说的从(的AssemblyName)方法,如果列表中已经包含其中assembly.AssemblyName.Name等于我通过(并AssemblyName.Name是的简单名称的任何组件首先检查的名称,即MyApp.Data,根据MSDN)。

流量经过了几个不重要的方法,降落在FindAssemblies方法。这种方法需要我传递的名称(我们已经看到的应该是一个的简单的程序集名称)。然后创建一个新的AssemblyName,与我们在作为的AssemblyName。codeBase的名字传递。

然后,它试图将程序集加载到一个临时的AppDomain。这是一个失败,出现上述异常的步骤。

显然,这是搜索的路径是错误的,但我无法通过从()方法提供一个路径。那也不行。

我已经尝试了一些其他FromXXX方法,但我已经得到无处花了太多时间在这了。该FromAssembliesInPath和FromAssembliesMatching也千万不要因为工作,再次,它是搜索的完全错误的目录。

呃......又是什么的问题:

任何人都可以解释如何得到Ninject公约的通过名称来加载程序集,而无需创建一个引用到装配,并通过指定一个包含的类型加载它?请。

我已经通过网页和 Ninject谷歌组页面已经搜查,我读过它的<一个HREF =htt​​p://innovatian.com/2010/02/ninject-extensions-conventions-$p$pview/>只(看起来是)有关文件并没有能够解决这个问题..呢。

解决方案

这个问题是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.

这篇关于如何使用(在其中一种或多种)Ninject约定延长而不引用大会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆