将数据传递给构造函数参数到城堡温莎容器
passing data to constructor parameters to castle windsor container
我想在 iocc 中将参数发送到 class 中的构造函数,但不要使用我的代码
这是我的代码:
public class InvestigationBox : IInvestigationBox
{
private long BoxId { get; set; }
private string BoxIP { get; set; }
private string BoxPort { get; set; }
public InvestigationBox(string boxip, int port)
{
this.BoxIP = boxip;
this.BoxPort = port;
}
} public interface IInvestigationBox
{}
这是我的温莎城堡配置
public static class CastleConfig
{
public static IWindsorContainer GetContainer()
{
var container = new WindsorContainer();
container.Register(Component.For<IInvestigationBox>()
.ImplementedBy<InvestigationBox>());
container.Register(Classes.FromThisAssembly().BasedOn<ApiController>().LifestylePerWebRequest());
return container;
}
}
并且我创建了用于为构造函数创建参数的工厂
此工厂代码:
public interface IInvestigationBoxFactory
{
IInvestigationBox Create(string boxip, int port);
}
这是我在 webapi 中的代码:
public class LoginCheckController : ApiController
{
private IHashing _ihashing { get; set; }
private ITokenValidation _itokenValidation { get; set; }
private IInvestigationBoxFactory _iInvestigationBoxFactory { get; set; }
private IInvestigationBox _iInvestigationBox { get; set; }
public LoginCheckController(IHashing ihashing, ITokenValidation itokenValidation, IInvestigationBoxFactory iInvestigationBoxFactory)
{
this._ihashing = ihashing;
this._itokenValidation = itokenValidation;
this._iInvestigationBoxFactory = iInvestigationBoxFactory;
}
public UserViewModel Get(string id, string id2)
{
_iInvestigationBox = _iInvestigationBoxFactory.Create("100.200", 1020);
}
}
但我在 运行 时间收到了这个错误:
无法创建组件 'SSM.WebApi.Controllers.LoginCheckController',因为它需要满足依赖关系。
'SSM.WebApi.Controllers.LoginCheckController' 正在等待以下依赖项:
- 未注册的服务 'SSM.WebApi.Infrastructure.IInvestigationBoxFactory'。
public class InvestigationBox : IInvestigationBox
{
private long BoxId { get; set; }
private string BoxIP { get; set; }
public InvestigationBox(string boxip, int port)
{
this.BoxIP = boxip;
this.BoxPort = port;
}
} public interface IInvestigationBox
{}
我没看到 this.BoxPort 在哪里初始化。也许你的意思是 this.BoxId?我是新手,但我会解决这个问题。
好的,这个 link 怎么样?
我认为重要的一点是:
We did add the class and interface to the solution, but we didn't register it. Let's quickly add an installer for it.
编辑:
添加工厂注册:
public static IWindsorContainer GetContainer()
{
var container = new WindsorContainer();
container.Register(Castle.MicroKernel.Registration.Component.For<IInvestigationBox>()
.ImplementedBy<InvestigationBox>());
container.Register(Castle.MicroKernel.Registration.Component.For<IInvestigationBoxFactory>()
.ImplementedBy<IInvestigationBoxFactory>());
container.Register(Classes.FromThisAssembly().BasedOn<ApiController>().LifestylePerWebRequest());
return container;
}
移动 _iInvastigationBox = _iInvestigationBoxFactory.Create("100.200", 1020);到构造函数,因为它无论如何都是硬编码的。
public class LoginCheckController : ApiController
{
private IInvestigationBoxFactory _iInvestigationBoxFactory { get; set; }
private IInvestigationBox _iInvestigationBox { get; set; }
public LoginCheckController(IInvestigationBoxFactory iInvestigationBoxFactory)
{
this._iInvestigationBoxFactory = iInvestigationBoxFactory;
_iInvestigationBox = _iInvestigationBoxFactory.Create("100.200", 1020);
}
}
您需要注册您的工厂
添加这个:
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For<IInvestigationBoxFactory>()
.AsFactory()
);
我想在 iocc 中将参数发送到 class 中的构造函数,但不要使用我的代码 这是我的代码:
public class InvestigationBox : IInvestigationBox
{
private long BoxId { get; set; }
private string BoxIP { get; set; }
private string BoxPort { get; set; }
public InvestigationBox(string boxip, int port)
{
this.BoxIP = boxip;
this.BoxPort = port;
}
} public interface IInvestigationBox
{}
这是我的温莎城堡配置
public static class CastleConfig
{
public static IWindsorContainer GetContainer()
{
var container = new WindsorContainer();
container.Register(Component.For<IInvestigationBox>()
.ImplementedBy<InvestigationBox>());
container.Register(Classes.FromThisAssembly().BasedOn<ApiController>().LifestylePerWebRequest());
return container;
}
}
并且我创建了用于为构造函数创建参数的工厂 此工厂代码:
public interface IInvestigationBoxFactory
{
IInvestigationBox Create(string boxip, int port);
}
这是我在 webapi 中的代码:
public class LoginCheckController : ApiController
{
private IHashing _ihashing { get; set; }
private ITokenValidation _itokenValidation { get; set; }
private IInvestigationBoxFactory _iInvestigationBoxFactory { get; set; }
private IInvestigationBox _iInvestigationBox { get; set; }
public LoginCheckController(IHashing ihashing, ITokenValidation itokenValidation, IInvestigationBoxFactory iInvestigationBoxFactory)
{
this._ihashing = ihashing;
this._itokenValidation = itokenValidation;
this._iInvestigationBoxFactory = iInvestigationBoxFactory;
}
public UserViewModel Get(string id, string id2)
{
_iInvestigationBox = _iInvestigationBoxFactory.Create("100.200", 1020);
}
} 但我在 运行 时间收到了这个错误:
无法创建组件 'SSM.WebApi.Controllers.LoginCheckController',因为它需要满足依赖关系。
'SSM.WebApi.Controllers.LoginCheckController' 正在等待以下依赖项: - 未注册的服务 'SSM.WebApi.Infrastructure.IInvestigationBoxFactory'。
public class InvestigationBox : IInvestigationBox
{
private long BoxId { get; set; }
private string BoxIP { get; set; }
public InvestigationBox(string boxip, int port)
{
this.BoxIP = boxip;
this.BoxPort = port;
}
} public interface IInvestigationBox
{}
我没看到 this.BoxPort 在哪里初始化。也许你的意思是 this.BoxId?我是新手,但我会解决这个问题。
好的,这个 link 怎么样?
我认为重要的一点是:
We did add the class and interface to the solution, but we didn't register it. Let's quickly add an installer for it.
编辑:
添加工厂注册:
public static IWindsorContainer GetContainer()
{
var container = new WindsorContainer();
container.Register(Castle.MicroKernel.Registration.Component.For<IInvestigationBox>()
.ImplementedBy<InvestigationBox>());
container.Register(Castle.MicroKernel.Registration.Component.For<IInvestigationBoxFactory>()
.ImplementedBy<IInvestigationBoxFactory>());
container.Register(Classes.FromThisAssembly().BasedOn<ApiController>().LifestylePerWebRequest());
return container;
}
移动 _iInvastigationBox = _iInvestigationBoxFactory.Create("100.200", 1020);到构造函数,因为它无论如何都是硬编码的。
public class LoginCheckController : ApiController
{
private IInvestigationBoxFactory _iInvestigationBoxFactory { get; set; }
private IInvestigationBox _iInvestigationBox { get; set; }
public LoginCheckController(IInvestigationBoxFactory iInvestigationBoxFactory)
{
this._iInvestigationBoxFactory = iInvestigationBoxFactory;
_iInvestigationBox = _iInvestigationBoxFactory.Create("100.200", 1020);
}
}
您需要注册您的工厂
添加这个:
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For<IInvestigationBoxFactory>()
.AsFactory()
);