上下文:我构建了一个处理“配置文件”对象的 REST 服务。每个配置文件都需要有一个唯一的名称。客户端出于验证目的需要执行的操作之一是检查以确保具有给定名称的配置文件不存在。
与其构建 RPC 样式的“ProfileExists”方法,我更愿意遵循 REST 设计原则并向具有给定名称的配置文件发出 HEAD 请求,然后根据配置文件是否已经存在返回适当的响应代码( 200、404),不需要响应体。
按照较新的 ServiceStack API 的约定,我设置了一个方法来接受 Head 请求,并使用 Fiddler 成功测试了这两种情况:
public object Head(GetProfile request)
{
ValidateRequest(request);
HttpStatusCode responseCode;
using (var scope = new UnitOfWorkScope())
{
responseCode = _profileService.ProfileExists(request.Name) ? HttpStatusCode.OK : HttpStatusCode.NotFound;
scope.Commit();
}
return new HttpResult { StatusCode = responseCode };
}
问题出在客户端。通过 ServiceStack 的 IRestClient 接口发出 HEAD 请求被证明是困难的。虽然有 Get、Post、Put 和 Delete 方法,但没有 Head 方法。从那里我假设我可以使用 CustomMethod 明确指定 HEAD 动词作为参数:
public bool ProfileExists(string profileName)
{
try
{
var response = _restClient.CustomMethod<IHttpResult>(HttpMethods.Head, new GetProfile { Name = profileName });
return response.StatusCode == HttpStatusCode.OK;
}
catch (WebServiceException ex)
{
if (ex.StatusCode == 404)
return false;
}
// Return false for any other reason right now.
return false;
}
但是,在验证 HttpVerb 参数时,底层实现 ( ServiceClientBase ) 会引发异常:
if (HttpMethods.AllVerbs.Contains(httpVerb.ToUpper()))
throw new NotSupportedException("Unknown HTTP Method is not supported: " + httpVerb);
集合HttpMethods.AllVerbs包含 RFC 2616 和更多的所有常用动词。除非此行为是错误,否则为任何已知 HTTP 动词引发异常表明作者对 CustomMethod 的意图不包括能够发出对已知 HTTP 动词的请求。
这引出了我的问题:如何在 ServiceStack 的客户端发出 HEAD 请求?