Shane
Shane

Reputation: 119

Adding a Service to ServiceStack

I am trying to add a new service to ServiceStack, but it is not being recognized, and my routes are not showing up in the metadata.

This is my service:

public class EventService : Service
{
    public object Post(EventRequest event_request)
    {
        return new EventResponse() {
             name = "FirstEvent"
        }
    }
}

public class EventRequest
{
    public int event_id { get; set; }
}

[Route("/event", "POST")]
public class EventResponse {
    public string name { get; set; }
}

I have even explicitly referenced the EventService in AppHost, even though they are in the same assembly. I am just adding a service to the basic service tutorial code, and my service is defined within the same namespace as the HelloService.

 public AppHost() //Tell ServiceStack the name and where to find your web services
        : base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly, typeof(EventService).Assembly) { }

I have also tried stopping and starting the IIS express service

What am I missing?

Upvotes: 1

Views: 271

Answers (1)

Scott
Scott

Reputation: 21521

It won't work because you have applied your [Route] to the wrong class. You need to have the route defined on the request DTO not the response DTO. So you should define it this way:

[Route("/event", "POST")]
public class EventRequest : IReturn<EventResponse>
{
    public int event_id { get; set; }
}

Your action method should define the return type too, rather than type object:

public class EventService : Service
{
    public EventResponse Post(EventRequest event_request)
    {
        return new EventResponse() {
             name = "FirstEvent"
        }
    }
}

You aren't getting metadata defined just now because there are no methods that are using your response EventResponse as a request DTO. So just a really minor thing causing your issue.


Old service assembly in bin:

Remove the SecondWbService.dll from your bin. This is an older service that is being loaded instead of MainWebService.dll - the one that you are actually editing and wanting to run. Because ServiceStack doesn't allow more than one AppHost, WebActivator is finding the older DLL and running it first, thus your service is hidden. After deleting that DLL, rerun the solution and it should be picked up correctly. You can confirm this by adding a breakpoint:

public AppHost() //Tell ServiceStack the name and where to find your web services
    : base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly, typeof(EventService).Assembly)
{ // BREAKPOINT HERE, confirm the assembly is loaded 
}

The metadata and service should then work correctly.

Upvotes: 2

Related Questions