我试图从这样的URL调用REST服务:
example.org/account/someusername
Run Code Online (Sandbox Code Playgroud)
我已经定义了请求和响应DTO.
[Route("/account/{UserName}", "GET")]
public class AccountRequest : IReturn<AccountResponse>
{
public string UserName { get; set; }
}
public class AccountResponse
{
public int Id { get; set; }
public string UserName { get; set; }
public string Bio { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
致电服务:
JsonServiceClient client = new JsonServiceClient("http://example.org");
AccountRequest request = new AccountRequest { UserName = "me" };
AccountResponse response = client.Get(request);
Run Code Online (Sandbox Code Playgroud)
但是当我在客户端上调用Get时,它不尊重该路由.当我在调试器中检查客户端实例时,AsyncOneWayBaseUri值为example.org/json/asynconeway/.这部分是无关紧要的,因为它并不意味着请求被发送到此URL.我实际上不知道它发送请求的位置.我没有得到任何错误,响应对象中的所有属性都为null.
我在这里错过了什么?
ServiceStack的服务客户端可以调用ServiceStack Web服务,因为它们支持ServiceStack的预定义路由,内置Auth,自动路由生成,内置错误处理等.
要调用第三方REST/HTTP Apis,您可以使用ServiceStack.Text附带的HTTP Utils,它为.NET的HttpWebRequest提供简洁,可读的令人愉快的API,用于常见的数据访问模式,例如:
List<GithubRepo> repos = "https://api.github.com/users/{0}/repos".Fmt(user)
.GetJsonFromUrl()
.FromJson<List<GithubRepo>>();
Run Code Online (Sandbox Code Playgroud)
我没有看到报告的行为,您是否在客户端上使用最新版本的ServiceStack?
测试生成的url(不进行服务调用)的TRequest.ToUrl(method)一种方法是直接调用扩展方法(Service Clients uss),例如
AccountRequest request = new AccountRequest { UserName = "me" };
request.ToUrl("GET").Print(); // /account/me
Run Code Online (Sandbox Code Playgroud)
当我尝试通过它调用它时,使用了相同的自动生成的路由JsonServiceClient,例如:
var client = new JsonServiceClient("http://example.org");
var response = client.Get(request); //calls http://example.org/account/me
Run Code Online (Sandbox Code Playgroud)
ServiceStack将尝试使用与您正在调用的DTO和HTTP方法中填充的值匹配的最合适的路由,如果没有匹配的路由,它将回退到预定义的路由.
默认情况下,将使用原始预定义路由:
/api/[xml|json|html|jsv|csv]/[syncreply|asynconeway]/[servicename]
Run Code Online (Sandbox Code Playgroud)
但ServiceStack现在还支持较短的别名/reply和/oneway,例如:
/api/[xml|json|html|jsv|csv]/[reply|oneway]/[servicename]
Run Code Online (Sandbox Code Playgroud)
您可以通过设置标志来选择在客户端中使用:
client.UseNewPredefinedRoutes = true;
Run Code Online (Sandbox Code Playgroud)
感谢大家的回答。C# 客户端从一开始就将请求发送到正确的地址,我用 Fiddler 对其进行了调试。只是我没有正确反序列化它。
帐户对象属于data响应的属性,而不是响应本身。客户端擅长使用 REST 服务,即使它们不是使用 ServiceStack 构建的。这很酷。
| 归档时间: |
|
| 查看次数: |
5620 次 |
| 最近记录: |