Headers for ServiceGateway

I’m using the service gateway to call services from other services. But I have one little nugget:

One service that I have returns images, so it returns a Stream. The mime-type is passed as the content-type http header. Works fine when calling from the browser for example.

How would I go about getting that info (the content type) when calling via the gateway? Since it returns a stream instead of a normal DTO-class… I could call it via HttpClient, but that’s a big overhead since it’s inside the same app, right?

Can you share code of your gateway and/or related services so there is a clearer picture of the problem?

So the service returning an image is setting the response header correctly, but you and you want to use the header information to inform how the consuming service should handle the stream response? If so, you could also get your IServiceGateway implementation to attach your required info to the Request.Response.Items dictionary which you could then read to know how to handle the Stream.

No sure if I’ve understood your setup correct, but hope that helps if I did, otherwise, if you could share your related gateway and services I might be able to point to other options.

Hi,
The service is defined as follows:

public Stream Get(GetImage req) {
  var outputStream = new MemoryStream();  // the image
  // ...
  base.Response.AddHeader("Content-Type", photo.MimeType);
  return outputStream;
}

And I’m calling it like this:

var gw = HostContext.AppHost.GetServiceGateway();
var stream = gw.Send<Stream>(new GetImage { Id = imageId, MaxWidth = 145 });

Through HTTP I would get the headers as well, but with the gw.Send() I don’t know how to do that.

So HTTP example is:

HttpClient client = new HttpClient();
  
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var contentType = response.GetHeader("Content-Type");
var stream = await response.Content.ReadAsStreamAsync();

The question is just how to modify my service so GW would work? Or else I can find some kind of workaround (using http would be one).

You should only be using the Service Gateway for sending typed Request/Response DTOs which can be sent with any Service Gateway, not requests which relies on using the In Process Gateway that manipulates the Request/Response context. i.e. Don’t use it for anything that needs more than the returned Typed Response DTO.

Instead have both Services call a shared implementation like an Extension method, alternatively you can invoke Services directly so the modifications to the Response Stream uses the same Request context:

using var imageService = base.ResolveService<GetImageService>();
var stream = imageService.Get(new GetImage { ... });

I changed my code, so that the bulk of the service is in just a regular method that I can call internally.

1 Like