ServiceStack Serviceless for CQRS - servicestack

I would like to be able to leverage ServiceStacks routing and built in documentation functionality to expose service operations based on the message type annotation with route and verb. A generic Service will simply delegate the received request to another Request Handling service.
eg.
Give I have the following type and configured routes with verb for each
public class Request{}
[Route("/order", "POST")]
public class PlaceOrder : Request { }
[Route("/order/{id}", "GET")]
public class OrderDetailsCriteria : Request {
Guid OrderId { get; set; }
}
public class OrderDetails { }
[Route("/inventoryItem/{id}", "PATCH")]
public class ReduceInventory : Request {
Guid InventoryItemId { get; set; }
}
I would like to create a simple service that will deal with all these but at the same time only allow the verbs as configured in the types attribute
something like:
public class MyService : Service {
public void Post(Request request) {
RequestService.Send(request);
}
public void Patch(Request request) {
RequestService.Send(request);
}
public object Get(Request request) {
return RequestService.Send(request);
}
}
Essentially I do not want to have to create a service or each set of Request types because each method will effectively be doing the same thing.

You can define a single implementation to handle all Verbs for a request by using the Any() method, e.g:
public object Any(MyRequest request)
{
var verb = base.Request.Verb; //if needed
...
RequestService.Send(request);
}

Related

Custom RazorPage get access to IRequest and Route Info

Using the Razor implementation for ServiceStack and AspNetCore how can I get access to the IRequest to get the route info for a custom razor page? Ultimately I want to get to the Name attribute on the route if possible.
public abstract class CustomView : RazorPage
{
public IRequest Req { get; set; } // always null
protected Breadcrumb Breadcrumb
{
get
{
return new Breadcrumb(this.Req); // need to pass IRequest to breadcrumbs so it can produce them
}
}
}
Routes are defined with a custom attribute which inherits from Route.
[CustomRoute("/message/{id}", View = "MessageDetailView", Name = "GetById")]
Custom Route:
public class CustomRoute : RouteAttribute
{
public string Name { get; set; }
public string View { get; set; }
public CustomRoute(string path) : base(path) { }
public CustomRoute(string path, string verbs) : base(path, verbs) { }
}
It seems that at least in web applications (as opposed to self hosted) the following works:
IRequest req = HostContext.TryGetCurrentRequest();

AmbiguousMatchException exception in ServiceStack?

PFB my code.
namespace ManualCSharpe
{
public class MyServices : Service
{
[Route("/L/hello/")] //RequestDTO one
public class HelloL
{
public string Name { get; set; }
}
[Route("/H/hello/")] //RequestDTO two
public class HelloH
{
public string Name1 { get; set; }
}
public class HelloResponse //ResponseDTO
{
public string Result { get; set; }
}
public class HelloServiceL : Service //Service One
{
public object Get(HelloL request)
{
return new HelloResponse { Result = "Low" };
}
}
public class HelloServiceH : Service //Service
{
public object Get(HelloH request)
{
return new HelloResponse { Result = "High" };
}
}
//Define the Web Services AppHost
public class AppHost : AppSelfHostBase
{
public AppHost()
: base("HttpListener Self-Host",new Assembly[] {typeof(HelloServiceL).Assembly, typeof(HelloServiceH).Assembly}) { }
public override void Configure(Funq.Container container) { }
}
//Run it!
static void Main(string[] args)
{
var listeningOn = args.Length == 0 ? "http://*:133/" : args[0];
var appHost = new AppHost()
.Init()
.Start(listeningOn);
Console.WriteLine("AppHost Created at {0}, listening on {1}",
DateTime.Now, listeningOn);
Console.ReadKey();
}
}
}
When I am tring to added two service then it is show below exception.
An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in ServiceStack.dll
Additional information: Could not register Request 'ManualCSharpe.MyServices+HelloL' with service 'ManualCSharpe.MyServices+HelloServiceL' as it has already been assigned to another service.
Each Request DTO can only be handled by 1 service.
I have below douts.
Here I have created two different DTO for Two Service. Why it is showing error like Each Request DTO can only be handled by 1 service. In simple word, Two route mapped with two DTO with two Service.
Can I create one route for multiple RequestDTO with multiple service? In Simple word, One Route/L/hello/ can be mapped with two DTO HelloL and HelloH.
You can't have Service class implementations nested inside another outer MyServices class:
public class MyServices : Service
{
[Route("/L/hello/")] //RequestDTO one
public class HelloL
{
public string Name { get; set; }
}
[Route("/H/hello/")] //RequestDTO two
public class HelloH
{
public string Name1 { get; set; }
}
public class HelloResponse //ResponseDTO
{
public string Result { get; set; }
}
public class HelloServiceL : Service //Service One
{
public object Get(HelloL request)
{
return new HelloResponse { Result = "Low" };
}
}
public class HelloServiceH : Service //Service
{
public object Get(HelloH request)
{
return new HelloResponse { Result = "High" };
}
}
}
Remove the outer MyServices class completely and just have the DTO's and Service classes directly under a C# namespace.
Also routes shouldn't end with a / suffix, so I'd change:
[Route("/L/hello/")]
to:
[Route("/L/hello")]
#mythz answer is correct for OP but I came here looking for an answer for a different situation which the cause was not particularly obvious - you will get this exception if you attempt to register the same assembly twice, for example, if you move a service implementation into the same assembly and were pulling it in like so:
public AppHost() : base("App", typeof(AdminService).GetAssembly(), typeof(InboundService).GetAssembly(),typeof(ProductService).GetAssembly())
For those of you who come here from a google search, a AmbiguousMatchException exception in ServiceStack can sometimes be triggered within ServiceStack but handled internally.
You can change your exception setting so it doesn't break on this exception.
I had changed my exception setting to break on all exceptions and this had me stuck for a while.

Servicestack routing problems

Hi i am new to servicestack
have a problem, with the the routing
i have mate a route
[Route("/Person/{ID}", "GET")]
public class GetPersonByID : IReturn<PersonResponse>
{
public decimal ObjectId { get; set; }
}
[Route("/Organization/{ID}/Person", "GET")]
public class GetPersonByOrganizationId : List<PersonResponse>
{
public decimal ObjectId { get; set; }
}
but then i am trying /Organization/281478302400588/Persons, I am getting a error saying
Unable to bind request
Stacktrace: at ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq,
IRestPath restPath) at
ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest httpReq,
IResponse httpRes, String operationName)
You need to ensure that the segment name in the route matches a property in the DTO. So {ID} should be {ObjectId}
In the second request, you should be using IReturn<List<PersonResponse>> rather than inheriting from List<PersonResponse> in your request
[Route("/Person/{ObjectId}", "GET")]
public class GetPersonByID : IReturn<PersonResponse>
{
public decimal ObjectId { get; set; }
}
[Route("/Organization/{ObjectId}/Person", "GET")]
public class GetPersonByOrganizationId : IReturn<List<PersonResponse>>
{
public decimal ObjectId { get; set; }
}
You also note you are trying /Organization/281478302400588/Persons You have used Persons in the request, but the route is Person so either change the request or the route accordingly. (Probably best on the route. i.e. [Route("/Organization/{ObjectId}/Persons", "GET")].
Then ensure in your service you are setting it up similar to this:
public class PersonService : Service
{
public PersonResponse Get(GetPersonByID request)
{
// return new PersonResponse();
}
public List<PersonResponse> Get(GetPersonByOrganizationId request)
{
// return new List<PersonResponse>();
}
}
I hope that helps.

ServiceStack: Pass an array to a Service

I'm having an issue when I pass an array to my service, it only recognizes the first value in the array:
Here is my request object:
[Route("/dashboard", "GET")]
public class DashboardRequest : IReturn<Dashboard>
{
public int[] EquipmentIds { get; set; }
}
Here is the request which is made:
http://localhost:9090/json/reply/DashboardRequest?EquipmentIds=1&EquipmentIds=2
But when I observe the array in my service, it only contains one value, which is 1.
public object Get(DashboardRequest request)
{
// request.EquipmentIds.Length == 1;
// request.EquipmentIds[0] == 1;
}
One Solution I've done is the following, which seems a bit hacky? I thought the point of specifying it in my Request Object is that I get a strongly typed Request Object?
var equipmentIds = Request
.QueryString["EquipmentIds"]
.Split(',')
.Select(int.Parse)
.ToList();
This works when you use the custom route, e.g:
[Route("/dashboard", "GET")]
public class DashboardRequest : IReturn<Dashboard>
{
public int[] EquipmentIds { get; set; }
}
and call it via the User Defined route, e.g:
http://localhost:9090/dashboard?EquipmentIds=1&EquipmentIds=2
Support for this has also been added on Predefined Routes in this commit which will be available from v4.0.24+ that's now available on MyGet.
So your existing request now works as well, e.g:
http://localhost:9090/json/reply/DashboardRequest?EquipmentIds=1&EquipmentIds=2
Bind the request object to the int array like
[Route("/dashboard/{EquipmentIds}", "GET")]
public class DashboardRequest : IReturn<Dashboard>
{
public int[] EquipmentIds { get; set; }
}
http://localhost:9090/dashboard/1,2

Accessing the calling Service from ServiceRunner?

I want to access the calling Service from inside the ServiceRunner OnBeforeRequest()method in order to get to an object in the calling service class. In MVC, I can create a class BaseController that overrides OnActionExecuting() and I can get to Data easily. However, using ServiceRunner, since it's not derived from Service, I don't see a way to get to the Service object.
Sample service:
public class ProductsService : Service
{
private MyData _data = new MyData();
public MyData Data
{
get { return _data; }
}
public object Get(GetProduct request)
{
// ...
return product;
}
}
In my custom ServiceRunner, how do I retrieve the ProductsService object from OnBeforeRequest() so I can get to Data?
public class MyServiceRunner<T> : ServiceRunner<T>
{
public override void OnBeforeExecute(IRequestContext requestContext, T request)
{
// var productService = ?
base.OnBeforeExecute(requestContext, request);
}
}
After much digging, it looks like this cannot be done. The Service action is available in the ServiceRunner as an unnamed lamdba delegate. There is no reference to the Service.
I have instead found a workaround. I first registered MyData in AppHost.Configure() using
container.RegisterAutoWired<MyData>();
I moved the MyData declaration to a filter attribute like this:
public class UseMyDataAttribute : RequestFilterAttribute
{
public MyData Data { get; set; } // injected by Funq IoC.
public override void Execute(IHttpRequest req, IHttpResponse res, object responseDto)
{
Data.SessionID = req.GetSessionId();
}
}
This way I can apply [UseMyData] to the ProductsService class and be able to set the Session ID to Data.

Resources