4

I’m using Service Stack, and I can´t (or I don´t know how make a Service class with constructor).

Here is what I did:

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

So, as I said, in the class ClientService, I wanna make a constructor, but when I make a request to the post message, it shows me the following error:

Error trying to resolve Service 'DexBrokerClient.Services.ClientService' or one of its auto wired dependencies

terbubbs
  • 1,512
  • 2
  • 25
  • 48
Mati Silver
  • 185
  • 1
  • 13

2 Answers2

4

ServiceStack Services are autowired and created using both Constructor and Property Injection. Any Constructor parameters are mandatory and must be registered in ServiceStack's IOC in AppHost.Configure(), e.g:

public override void Configure(Container container)
{
    container.Register<ILayout>(c => ...);
    container.Register<Init>(c => ...);
}

Whereas any public properties on Services are optional and will be populated from the IOC if they exist or are otherwise null.

Resolving Services

Services should be resolved using base.ResolveService<T> API's when inside Service class or HostContext.ResolveService<T>() singleton outside of Services. This resolves the Service class from ServiceStack's IOC and is populated with the IRequest context (if any).

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks, I still not understand how to solve this.. In the class "Context" i´m creating the instance of the Form and the Init (class), Also, I create the instance of the ClientService, which I send the parameters to the constructor.. – Mati Silver Apr 11 '16 at 12:54
  • @MatiSilver I don't know what you're trying to do or why you're using a Service class for this? Services are pre-autowired when the AppHost is initialized and need to be resolved from the IOC, if you're using a non default constructor each of the params need to be registered in the IOC. You shouldn't create a Service class directly, this is what ServiceStack does this on its own when it executes the Service. You can resolve the Service class from the IOC to create an instance of it, but it needs its mandatory deps registered in the IOC, if you don't want to do this, don't use a Service class. – mythz Apr 11 '16 at 13:10
  • Ok, lets do it easier.. in my Context class, I´ll create an object which i need to use in the Service class. (I change my architecture, so now i don´t have a constructor with parameter), but i still need to use the object which I initialize in other class.. how cshould I do to use the object which i created in one class, into service class? Thanks – Mati Silver Apr 11 '16 at 13:49
  • 1
    A more complete answer would be great. You do not actually explain what to do. Coming from someone who has never used service stack until today this answer doesn't give me anything. What goes in `(c => ...)`? – Agrejus Jul 25 '17 at 15:18
2

@Mati, @TheMiddleMan The @mythz's answer expects you to have some general knowledge about dependency injection and containers that is IOC (inversion of control). In your question you ask about constructor injection. ServiceStack's IOC can handle it using auto-wiring if you have pre-configured (registered) the parameter object, for example:

container.Register(c => new Layout()); For more samples see https://docs.servicestack.net/ioc.

More about IOC and DI (dependency injection) can be read from https://dzone.com/articles/ioc-vs-di.

J Pollack
  • 2,788
  • 3
  • 29
  • 43