Differentiating the type of client making a request. HTTP vs gRPC vs MQ, etc. #47
-
I've got a basic service that just needs to sling around a few different DTO POCOs. The DTO POCOs don't matter for the purpose of the question so I'm just going to use the template/default "Hello" DTO to illustrate the question. We have the basic (gRPC ready) Hello DTO POCOs. [Route("/hello")]
[Route("/hello/{Name}")]
[DataContract]
public class Hello : IReturn<HelloResponse>
{
[DataMember(Order = 1)]
public string Name { get; set; }
}
[DataContract]
public class HelloResponse
{
[DataMember(Order = 1)]
public string Result { get; set; }
[DataMember(Order = 2)]
public ResponseStatus ResponseStatus { get; set; }
} And in adition to configuring/enabling gRPC services we have also added a message queue request handler for it inside mqServer.RegisterHandler<Hello>(appHost.ExecuteMessage); And we have our basic service to respond to any public class MyServices : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = $"Hello, {request.Name}!" };
}
} With everything all setup I'm able to generate my gRPC and MQ clients for calling this API, the issue is how to tell these two kinds of requests apart? What I need is a mechanism to do this (pseudocode due to not knowing the correct mechanism)... public class MyServices : Service
{
public object Any(Hello request)
{
if (RequestedByGrpcClient(request)){
// Handle requests from gRPC clients.
return new HelloResponse { Result = $"Hello brought to you via gRPC, {request.Name}!" };
}
if (RequestedByMessageQueueClient(request)){
// Handle requests from gRPC clients.
return new HelloResponse { Result = $"Hello brought to you via message queuing, {request.Name}!" };
}
return new HelloResponse { Result = $"Hello, {request.Name}!" };
}
} Can I acommplish this? If so, how? I breifly thought about creating another entirely duplicted set of DTO POCOs types to differentiate between gRPC and Message Queuing, but this would be an essentially identical POCO, I could remove the gRPC annotations to make it look more different, but creating what is for all intents and purposes a duplicate of the gRPC POCO, feels like a pretty massive violaton of the DRY principle... and since these data types are already expressed as Entity Framework partial classes, ServiceStack gRPC DTO POCOs and OrmLite model POCOs, I'd like to try and avoid repeating myself again unless necessary. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ok so looks like you just want to differentiate which endpoint the API was invoked on. Firstly the Order of Operations docs explains the Request pipeline that each endpoint executes, i.e: The first way to tell difference between them is that the
So you could detect different endpoints with: if (base.Request is GrpcRequest) {} //gRPC
else if (base.Request is BasicRequest) {} //MQ
else if (base.Request is NetCoreRequest) {} //HTTP on ASP.NET Core Although I'd recommend using the Request Attributes for a non concrete type check to detect the type of request, e.g: if (base.Request.RequestAttributes.HasFlag(RequestAttributes.Grpc)) {}
else if (base.Request.RequestAttributes.HasFlag(RequestAttributes.MessageQueue)) {}
else if (base.Request.RequestAttributes.HasFlag(RequestAttributes.Http)) {} |
Beta Was this translation helpful? Give feedback.
Ok so looks like you just want to differentiate which endpoint the API was invoked on.
Firstly the Order of Operations docs explains the Request pipeline that each endpoint executes, i.e:
The first way to tell difference between them is that the
IRequest
context frombase.Request
will use a different impl per endpoint:NetCoreRequest
- HTTP on ASP.NET CoreBasicRequest
- MQGrpcRequest
- gRPCSo you could detect different endpoints with:
Although I'd …