• StructureMap


    In one of my projects (.NET based - using the Web API), I am using StructureMap as a dependency injection tool. The basic setup I have for it is that for each assembly where dependency injection is required, I have a dependency resolution class which extends the StructureMap Registry class. Here is a simple and clear example:

    namespace Security.DependencyResolution
    {
        public class SecurityRegistry : Registry
        {
            public SecurityRegistry()
            {
                Scan(config =>
                            {
                                config.WithDefaultConventions();
                                config.AssembliesFromApplicationBaseDirectory();
                                config.IncludeNamespace("Data.Repositories");
                                config.IncludeNamespace("Security");
                            });
                        }
     
            public static void Initialize(IContainer container)
            {
                container.Configure(ct => ct.Scan(scan =>
                                                    {
                                                        scan.LookForRegistries();
                                                        scan.TheCallingAssembly();
                                                    }));
            }
        }
    }

    I want to focus on the AssembliesFromApplicationBaseDirectory method since this is the one that caused me some head pain today. When AssembliesFromApplicationBaseDirectory gets invoked, the base directory of the current application domain is traversed, and any assembly found is added to the scanning operation. Note that I am also using WithDefaultConventions, which informs the scanner that any concrete class named Product , for example, implements an interface named IProduct , and it gets added to PluginType IProduct . This will spare you from using explicit declarations of PluginTypes for <IProduct>().Use<Product>() .

    Because the application has multiple layers, the dependencies are quite intricate.  This is why I thought that if I used AssembliesFromApplicationBaseDirectory in the scanner configuration, it would be a great idea. Well, I was wrong, because I ended up with the following exception, which was thrown every time StructureMap was bootstrapping:

    StructureMap configuration failures: Error: 170 Source: Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223 Unable to find the exported Type's in assembly System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. One or more of the assembly's dependencies may be missing. Could not load file or assembly 'System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) System.IO.FileLoadException: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. It was obvious that StructureMap was checking "all" assemblies from the base directory of current application domain. That is not what I intended to happen, there are system assemblies which are not needed in the dependency injection. I might say that I was lucky that this exception came to light, because this determined me to add some filtering into the process. If you check StructureMap documentation you will notice that AssembliesFromApplicationBaseDirectory has an overload which accepts a predicate, based on it, it will filter the assemblies which should be added to the scanning operation. So what I did is that I have defined a local array which holds the name of the assemblies which should be added to the scanning operation and used it in the filtering predicate.


    It was obvious that StructureMap was checking "all" assemblies from the base directory of the current application domain. That is not what I intended.  There are system assemblies that are not needed in the dependency injection. I might say that I was lucky that this exception came to light, because this made me determined to add some filtering into the process. If you check StructureMap documentation, you will notice that AssembliesFromApplicationBaseDirectory has an overload that accepts a predicate.  Based on the predicate, it will filter the assemblies that should be added to the scanning operation. So what I did is I have defined a local array that holds the name of the assemblies that should be added to the scanning operation and used it in the filtering predicate.

    namespace  Security.DependencyResolution
    {
        public class SecurityRegistry : Registry
        {
            public SecurityRegistry()
            {
                var assemblies = new[] { "Data.Repositories", " Security" };
                Scan(config =>
                            {
                                config.AssembliesFromApplicationBaseDirectory(assembly => assemblies.Contains(assembly.FullName));
                                config.IncludeNamespace("Data.Repositories");
                                config.IncludeNamespace("Security");
                            });
                        }
     
            public static void Initialize(IContainer container)
            {
                container.Configure(ct => ct.Scan(scan =>
                                                    {
                                                        scan.LookForRegistries();
                                                        scan.TheCallingAssembly();
                                                    }));
            }
        }
    }


    Besides that, I decided to drop WithDefaultConventions in favor of an explicit declaration. At least this will make things more obvious, even though it is more verbose. The conclusion is... don't take shortcuts, not unless you are 100% sure it will work properly under any circumstances.

    Published at DZone with permission of its author, Mihai Huluta .

    (Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

  • 相关阅读:
    04-修改域控的操作主机(主备切换)
    03-域控上删除组织单位的错误
    02-搭建域控的从节点
    01-域控服务器的搭建
    06-"Login failed for user 'NT AUTHORITYSYSTEM'. 原因: 无法打开明确指定的数据库。[客户端:<local machine>]"异常处理
    04-数据库范式
    9-系统交互式登录无需按Ctrl+Alt+Del的策略启用
    05-拒绝了对对象'server'的VIEW SERVER STATE权限
    37-SQLServer的审核/审计功能介绍
    微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)
  • 原文地址:https://www.cnblogs.com/lit10050528/p/3400998.html
Copyright © 2020-2023  润新知