简体   繁体   中英

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:

  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. 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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