【发布时间】:2016-02-10 00:17:32
【问题描述】:
我正在构建一个带有 NServiceBus 的Request/Acknowledge/Poll style REST service,以管理队列处理。我想给客户端一个 URI 来轮询更新。
因此,我想在我的 Web 服务中返回一个 location 标头元素作为确认的一部分。我可以看到可以这样做:
return new HttpResult(response, HttpStatusCode.Accepted)
{
Location = base.Request.AbsoluteUri.CombineWith(response.Reference)
}
但是对于诸如 http://localhost:54567/approvals/?message=test 这样的 Url,它会创建一条新消息(我知道我应该只使用 POST),位置将返回为:http://localhost:54567/approvals/?message=test/8f0ab1c1a2ca46f8a98b75330fd3ac5c。
ServiceStack 请求不公开 Uri 片段,仅公开 AbsouteUri。这意味着我需要访问原始请求。无论是在 IIS 中还是在自托管进程中运行,我都希望它能够正常工作。我能想到的最接近的是以下内容,但看起来很笨拙:
var reference = Guid.NewGuid().ToString("N");
var response = new ApprovalResponse { Reference = reference };
var httpRequest = ((System.Web.HttpRequest)base.Request.OriginalRequest).Url;
var baseUri = new Uri(String.Concat(httpRequest.Scheme, Uri.SchemeDelimiter, httpRequest.Host, ":", httpRequest.Port));
var uri = new Uri(baseUri, string.Format("/approvals/{0}", reference));
return new HttpResult(response, HttpStatusCode.Accepted)
{
Location = uri.ToString()
};
现在返回:http://localhost:55847/approvals/8f0ab1c1a2ca46f8a98b75330fd3ac5c
有什么建议吗?无论 ServiceStack 是如何托管的,这都能正常工作吗?我有点害怕System.Web.HttpRequest 转换成self hosted process。这段代码安全吗?
【问题讨论】:
标签: servicestack