c# – 带构造函数的ServiceStack Service类
作者:互联网
我正在使用Service Stack,但我无法(或者我不知道如何使用构造函数创建Service类).
这是我做的:
public class AppHost : AppSelfHostBase
{
public AppHost()
: base("ClientService", typeof(ClientService).Assembly)
{
}
public override void Configure(Container container)
{
}
}
public class Context : ApplicationContext
{
//This class is invoked from program.cs (winform application)
//Here i have the logic of what form should i start.
var listeningOn = string.Format("http://*:{0}/", port);
var appHost = new AppHost();
appHost.Init();
appHost.Start(listeningOn);
ClientService cl = new ClientService((ILayout)f, i);
}
public class ClientService : Service
{
private readonly ILayout _activeForm;
private static Init _initConf;
public ClientService(ILayout f, Init i)
{
_activeForm = f;
_activeForm.GetClientData(i);
_initConf = i;
}
public HttpResult Post(Person request)
{
//Here i use _activeForm and _initConf
}
}
所以,正如我所说,在ClientService类中,我想创建一个构造函数,但是当我向post消息发出请求时,它会显示以下错误:
Error trying to resolve Service
‘DexBrokerClient.Services.ClientService’ or one of its auto wired
dependencies
解决方法:
ServiceStack服务是自动装配的,并使用构造函数和属性注入创建.任何Constructor参数都是必需的,必须在AppHost.Configure()中的ServiceStack的IOC中注册,例如:
public override void Configure(Container container)
{
container.Register<ILayout>(c => ...);
container.Register<Init>(c => ...);
}
服务上的任何公共属性都是可选的,如果存在,则将从IOC填充,否则为空.
解决服务
应使用base.ResolveService< T>解决服务.在服务类内部或者在服务外部的HostContext.ResolveService< T>()单例时的API.这将从ServiceStack的IOC中解析Service类,并使用IRequest上下文(如果有)填充.
标签:c,ioc-container,servicestack,funq 来源: https://codeday.me/bug/20190628/1311948.html