简体   繁体   English

StructureMap ASP.net MVC不注入接口

[英]StructureMap ASP.net MVC Not Injecting Interface

I have tried to get the structure map dependency to work correctly but it is not working correctly when putting an interface in the constructor versus putting the class name. 我试图使结构图依赖关系正常工作,但是在将接口放入构造函数中而不是放置类名称时,它不能正确工作。

The following code works: 以下代码有效:

public class HomeController : Controller
{
    private readonly MyService _service;

    public HomeController(MyService service)
    {
        _service = service;
    }

    public ActionResult Index()
    {
        return View();
    }
}

public class MyService : IService
{
    public string GetName()
    {
        return "Hello";
    }
}

public interface IService
{
    string GetName();
}

But the following code does NOT work: 但是以下代码不起作用:

public class HomeController : Controller
{
    private readonly IService _service;

    public HomeController(IService service)
    {
        _service = service;
    }

    public ActionResult Index()
    {
        return View();
    }
}

public class MyService : IService
{
    public string GetName()
    {
        return "Hello";
    }
}

public interface IService
{
    string GetName();
}

Here is the logic from the DependencyResolution class: 这是DependencyResolution类的逻辑:

  public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });
        //                x.For<IExample>().Use<Example>();
                    });
        return ObjectFactory.Container;
    }

I am using the StructureMap.MVC4 nuget package to setup the dependency injection. 我正在使用StructureMap.MVC4 nuget包来设置依赖项注入。 What am I doing wrong? 我究竟做错了什么?

In your calling assembly if you have only single implementation class representing a inteface you can use like below 在调用程序集中,如果只有一个实现接口代表一个接口,则可以像下面这样使用

x.Scan(scan =>
             {
               scan.TheCallingAssembly();
               scan.WithDefaultConventions();
               scan.SingleImplementationsOfInterface();
             });

without SingleImplementationsOfInterface() method structuremap can not identify the proper implementation class for the IService interface . 如果没有SingleImplementationsOfInterface()方法,则structuremap无法识别IService接口的正确实现类。

or 要么

you can map like below 你可以像下面这样映射

ObjectFactory.Initialize(x =>
        {
            x.Scan(scan =>
            {
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();
            });
            x.For<IService>().Use<MyService>();
        });

try this piece of code instead: 试试下面这段代码:

 public class MvcBootStrapper
        {
            public static void ConfigurationStructureMap()
            {
                ObjectFactory.Initialize(x =>
                {
                    x.AddRegistry<MyService>();
                });
            }
        }

and finally register you classes and interfaces like this: 最后注册您的类和接口,如下所示:

 public class SampleRegistery : Registry
        {
            public SampleRegistery ()
            {
                ForRequestedType<IService>().TheDefaultIsConcreteType<MyService>();
            }
        }

see this article for more info. 看到文章获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM