StructureMap 异常 202 未定义默认实例(版本 2.61)

发布于 2024-10-16 00:24:04 字数 2302 浏览 3 评论 0原文

容器初始化:

public override void Initialize()
        {

            ObjectFactory.Initialize(x =>
                                         {
                                             x.Scan(s =>
                                                        {
                                                            s.TheCallingAssembly();
                                                            s.AssemblyContainingType<IRegistar>();
                                                            s.WithDefaultConventions();

                                                        });

                                         });

        }

Global.asax.xc

  var dependencyContainer = new DependencyContainer();
            dependencyContainer.Initialize();

            ControllerBuilder.Current.SetControllerFactory(new DependencyControllerFactory());

获取实例:

public class DependencyControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                return base.GetControllerInstance(requestContext, controllerType);
            }

            try
            {
                return ObjectFactory.GetInstance(controllerType) as Controller;
            }
            catch (StructureMapException exception)
            {
                Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw;
            }
        }
    }

控制器

private IBabyRepository _babyReposoitory; {

  public BabyController(IBabyRepository babyRepository)
  {
      _babyReposoitory = babyRepository;
  }

类尝试 DI/Ioc

public class BabyRepository : IBabyRepository, IRepository<Baby>, IRegistar
{
    Just a Default constructor here();
    code...
}

错误:

StructureMap 异常代码:202 没有为 PluginFamily Domain.Repository.IBabyRepository、Domain、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null 定义默认实例

Me

我做错了什么?

Initialization of Container:

public override void Initialize()
        {

            ObjectFactory.Initialize(x =>
                                         {
                                             x.Scan(s =>
                                                        {
                                                            s.TheCallingAssembly();
                                                            s.AssemblyContainingType<IRegistar>();
                                                            s.WithDefaultConventions();

                                                        });

                                         });

        }

Global.asax.xc

  var dependencyContainer = new DependencyContainer();
            dependencyContainer.Initialize();

            ControllerBuilder.Current.SetControllerFactory(new DependencyControllerFactory());

The Get Instance:

public class DependencyControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                return base.GetControllerInstance(requestContext, controllerType);
            }

            try
            {
                return ObjectFactory.GetInstance(controllerType) as Controller;
            }
            catch (StructureMapException exception)
            {
                Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw;
            }
        }
    }

The Controller

private IBabyRepository _babyReposoitory;
{

  public BabyController(IBabyRepository babyRepository)
  {
      _babyReposoitory = babyRepository;
  }

Class trying to DI/Ioc

public class BabyRepository : IBabyRepository, IRepository<Baby>, IRegistar
{
    Just a Default constructor here();
    code...
}

The Error:

StructureMap Exception Code: 202
No Default Instance defined for PluginFamily Domain.Repository.IBabyRepository, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Me

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-10-23 00:24:04

您不告诉 StructureMap 如何创建 BabyRepository。

我认为你的扫描需要是这样的:

        Scan(y =>
        {
            y.AssemblyContainingType<IRegistar>();
            y.Assembly(Assembly.GetExecutingAssembly().FullName);
            y.With(new RepositoryScanner());
        })

然后需要编写自定义 RepositoryScanner 的代码:

public class RepositoryScanner : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        if (type.BaseType == null) return;

        if (type.GetInterface(typeof(IRepository).Name) != null)
        {
            var name = type.Name;

            registry
               .For<IRepository>()
               .AddInstances(y => y.Instance(new ConfiguredInstance(type).Named(name)));
        }
    }
}

You don't tell StructureMap how to create BabyRepository.

I think your scan needs to be something like this:

        Scan(y =>
        {
            y.AssemblyContainingType<IRegistar>();
            y.Assembly(Assembly.GetExecutingAssembly().FullName);
            y.With(new RepositoryScanner());
        })

Then need to code up the custom RepositoryScanner:

public class RepositoryScanner : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        if (type.BaseType == null) return;

        if (type.GetInterface(typeof(IRepository).Name) != null)
        {
            var name = type.Name;

            registry
               .For<IRepository>()
               .AddInstances(y => y.Instance(new ConfiguredInstance(type).Named(name)));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文