{"id":202044,"date":"2013-11-13T19:14:02","date_gmt":"2013-11-13T15:14:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=202044"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=202044","title":{"rendered":"<span class=\"post_title\">\u041a\u0430\u043a ExpressionTrees \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c WebApi<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u043c \u0445\u043e\u0440\u043e\u0448\u0438 ApiController&#8217;\u044b, \u0434\u0430 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u043e\u043d\u0438 WSDL \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0432\u0437\u044f\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c proxy. \u0414\u0430, ApiController&#8217;\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f unit-test&#8217;\u0430\u043c\u0438. \u041d\u043e \u044e\u043d\u0438\u0442\u044b \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0438 \u0432 \u0446\u0435\u043b\u043e\u043c \u0431\u0435\u0437 \u043f\u0430\u0440\u043e\u0447\u043a\u0438 end-to-end \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u043a\u0430\u043a-\u0442\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u041c\u043e\u0436\u043d\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0441\u043c\u0438\u0440\u0438\u0442\u044c\u0441\u044f, \u0432\u0437\u044f\u0442\u044c HttpClient \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code>HttpClient client = new HttpClient(); client.BaseAddress = new Uri(&quot;http:\/\/localhost:56851\/&quot;);  \/\/ Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(     new MediaTypeWithQualityHeaderValue(&quot;application\/json&quot;));  HttpResponseMessage response = client.GetAsync(&quot;api\/User&quot;).Result;  if (response.IsSuccessStatusCode) {     var users = response.Content.ReadAsAsync&     lt;IEnumerable&lt;Users&gt;&gt;().Result;     usergrid.ItemsSource = users;  } else {     MessageBox.Show(&quot;Error Code&quot; +      response.StatusCode + &quot; : Message - &quot; + response.ReasonPhrase); } <\/code><\/pre>\n<p>  \u041d\u043e \u043a\u0430\u043a \u0436\u0435 \u044d\u0442\u043e \u043c\u0443\u0442\u043e\u0440\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043b\u0435\u0437\u0442\u044c \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0442\u0438\u043f\u044b, \u043a\u043e\u0440\u043e\u0447\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u043e\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code>var resp = GetResponse&lt;SomeController&gt;(c =&gt; gc.SomeAction(new Dto{val = &quot;123&quot;})); <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u044d\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 <s>\u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0438\u0447\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438<\/s> \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h1>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e\u0431 API<\/h1>\n<p>\u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0435 API \u0432\u043e\u043e\u0431\u0449\u0435 \u0435\u0441\u0442\u044c, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043c\u0430\u043f\u0438\u043c \u0440\u043e\u0443\u0442\u044b  <\/p>\n<pre><code>[SetUp] public void SetUp() {     _cfg = new HttpConfiguration();     _cfg.Routes.MapHttpRoute(             name: &quot;DefaultApi&quot;,         routeTemplate: &quot;api\/{controller}\/{action}\/{id}&quot;,         defaults: new { id = RouteParameter.Optional }     ); } <\/code><\/pre>\n<p>  <\/p>\n<h1>\u0412\u044b\u0437\u043e\u0432 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430<\/h1>\n<p>ApiDescriptions \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u043d\u0430\u0435\u0442 \u043e \u0442\u043e\u043c, \u0433\u0434\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0438 \u043b\u044e\u0431\u0435\u0437\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0412 WebApi \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430: \u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0434\u0432\u0430 Http-\u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 API, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u043e\u0442 \u043a\u0435\u0439\u0441 \u043c\u0435\u043d\u044f \u043d\u0435 \u0432\u043e\u043b\u043d\u0443\u0435\u0442. \u0421 \u0447\u0438\u0441\u0442\u043e\u0439 \u0441\u043e\u0432\u0435\u0441\u0442\u044c\u044e \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434  <\/p>\n<pre><code>protected HttpResponseMessage GetResponse&lt;T&gt;(Expression&lt;Action&lt;T&gt;&gt; expression)     where T : ApiController {     var baseAddress = System.Configuration.ConfigurationManager.AppSettings[&quot;BaseAddress&quot;];     var convert = (MethodCallExpression)expression.Body;     var name = convert.Method.Name;     var paramsName = convert.Method.GetParameters().Select(p =&gt; p.Name).ToArray();      var desc = _cfg.Services.GetApiExplorer().ApiDescriptions.First(         d =&gt;             d.ActionDescriptor.ControllerDescriptor.ControllerType == typeof(T) &&             d.ActionDescriptor.ActionName == name);   \/\/... <\/code><\/pre>\n<p>  \u0414\u043e\u043f\u0443\u0449\u0435\u043d\u0438\u0435 \u21162. \u041a\u0440\u043e\u043c\u0435 JSON \u043c\u043d\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e. \u0414\u043b\u044f get-\u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0438 post-\u0441 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430\u043c\u0438 \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u0430 paramName={paramName} \u043d\u0430 paramName=\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 Expression, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438.  <\/p>\n<pre><code>using (var client = new HttpClient { BaseAddress = new Uri(baseAddress) }) {     client.DefaultRequestHeaders.Accept.Add(         new MediaTypeWithQualityHeaderValue(&quot;application\/json&quot;));      var relPath = desc.RelativePath;      var index = 0;      if (relPath.Contains(&quot;?&quot;))     {         foreach (var pars in paramsName)         {             relPath = relPath.Replace(                 string.Format(&quot;{{{0}}}&quot;, pars.Name),                 InvokeExpression(convert.Arguments[index++], pars.ParameterType).Return(o =&gt; o.ToString(), string.Empty));         }      } <\/code><\/pre>\n<p>  <\/p>\n<h1>InvokeExpression<\/h1>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043b\u044e\u0431\u043e\u0433\u043e Expression \u2014 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043b\u044f\u043c\u0431\u0434\u0443, \u0447\u0442\u043e \u044f \u0438 \u0441\u0434\u0435\u043b\u0430\u043b. \u041e\u0442\u043a\u0440\u043e\u0432\u0435\u043d\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043c\u044b \u0437\u043d\u0430\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438\u0437 \u0442\u0438\u043f\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430. \u041d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u0435\u0439\u0441 \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0445 void. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Action \u0432\u043c\u0435\u0441\u0442\u043e Func&lt;T,TResult&gt;. \u0422\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043b\u043e\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f. \u0417\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u044f \u043d\u0435 \u0433\u043e\u043d\u044e\u0441\u044c, \u2014 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0438\u0437\u0434\u0435\u0440\u0436\u043a\u0438 \u0441\u043e\u0436\u0440\u0443\u0442 \u0432\u0441\u0435 \u0442\u0435 \u043d\u0430\u043d\u043e\u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u043b\u0435\u043d\u044b \u043d\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.  <\/p>\n<pre><code>private static object InvokeExpression(Expression e, Type returnType) {     return Expression.Lambda(         typeof (Func&lt;&gt;).MakeGenericType(returnType),         e).Compile().DynamicInvoke(); } <\/code><\/pre>\n<p>  <\/p>\n<h1>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/h1>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0441\u0430\u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434. \u0414\u043b\u044f Post \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442-wrapper. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438\u043b\u0438 \u043f\u0430\u0434\u0430\u0435\u043c \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439.  <\/p>\n<pre><code>var uri = new Uri(new Uri(baseAddress), relPath);  var resp = desc.HttpMethod.Method == HttpMethod.Post.ToString()     ? client.PostAsJsonAsync(uri.ToString(), InvokeExpression(convert.Arguments.Single(),     desc.ParameterDescriptions.Single().ParameterDescriptor.ParameterType)).Result     : client.GetAsync(uri).Result;  if (resp.StatusCode == HttpStatusCode.InternalServerError) {     using (var sr = new StreamReader(resp.Content.ReadAsStreamAsync().Result))     {         throw new InvalidOperationException(sr.ReadToEnd());     }                     } return resp; <\/code><\/pre>\n<p>  <\/p>\n<h1>\u0412 \u0438\u0442\u043e\u0433\u0435<\/h1>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434  <\/p>\n<pre><code>protected HttpResponseMessage GetResponse&lt;T&gt;(Expression&lt;Action&lt;T&gt;&gt; expression)     where T : ApiController {     var baseAddress = System.Configuration.ConfigurationManager.AppSettings[&quot;BaseAddress&quot;];     var convert = (MethodCallExpression)expression.Body;     var name = convert.Method.Name;     var paramsName = convert.Method.GetParameters().ToArray();      var desc = _cfg.Services.GetApiExplorer().ApiDescriptions.First(         d =&gt;             d.ActionDescriptor.ControllerDescriptor.ControllerType == typeof(T) &&             d.ActionDescriptor.ActionName == name);      using (var client = new HttpClient { BaseAddress = new Uri(baseAddress) })     {         client.DefaultRequestHeaders.Accept.Add(             new MediaTypeWithQualityHeaderValue(&quot;application\/json&quot;));          var relPath = desc.RelativePath;          var index = 0;          if (relPath.Contains(&quot;?&quot;))         foreach (var pars in paramsName)         {             relPath = relPath.Replace(                 string.Format(&quot;{{{0}}}&quot;, pars.Name),                 InvokeExpression(convert.Arguments[index++], pars.ParameterType).Return(o =&gt; o.ToString(), string.Empty));         }                          var uri = new Uri(new Uri(baseAddress), relPath);          var resp = desc.HttpMethod.Method == HttpMethod.Post.ToString()             ? client.PostAsJsonAsync(uri.ToString(), InvokeExpression(convert.Arguments.Single(),             desc.ParameterDescriptions.Single().ParameterDescriptor.ParameterType)).Result             : client.GetAsync(uri).Result;          if (resp.StatusCode == HttpStatusCode.InternalServerError)         {             using (var sr = new StreamReader(resp.Content.ReadAsStreamAsync().Result))             {                 throw new InvalidOperationException(sr.ReadToEnd());             }                              }         return resp;     } } <\/code><\/pre>\n<p>  \u041c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e, \u043d\u043e \u0441\u0432\u043e\u044e \u0446\u0435\u043b\u044c \u043e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442, \u0442\u0435\u043f\u0435\u0440\u044c \u044f \u043c\u043e\u0433\u0443 \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0442\u0435\u0441\u0442\u044b:  <\/p>\n<pre><code>[Test] public void UserController_TokenValid_WrongTokenReturnFalse() {     var resp = GetResponse&lt;UserController&gt;(gc =&gt; gc.TokenValid(&quot;123&quot;));     Assert.AreEqual(false, resp.Content.ReadAsAsync&lt;bool&gt;().Result); } <\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u0438\u0435:  <\/p>\n<pre><code>var obj = new RoundResultDtoIn() {     LevelId = 3,     RoomName = &quot;123&quot;,     RoundTime = 50,     StartDateTime = DateTime.Now };              GetResponse&lt;GameController&gt;(gc =&gt; gc.SaveResults(obj)); <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/202044\/\"> http:\/\/habrahabr.ru\/post\/202044\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u043c \u0445\u043e\u0440\u043e\u0448\u0438 ApiController&#8217;\u044b, \u0434\u0430 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u043e\u043d\u0438 WSDL \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0432\u0437\u044f\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c proxy. \u0414\u0430, ApiController&#8217;\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f unit-test&#8217;\u0430\u043c\u0438. \u041d\u043e \u044e\u043d\u0438\u0442\u044b \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0438 \u0432 \u0446\u0435\u043b\u043e\u043c \u0431\u0435\u0437 \u043f\u0430\u0440\u043e\u0447\u043a\u0438 end-to-end \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u043a\u0430\u043a-\u0442\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u041c\u043e\u0436\u043d\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0441\u043c\u0438\u0440\u0438\u0442\u044c\u0441\u044f, \u0432\u0437\u044f\u0442\u044c HttpClient \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code>HttpClient client = new HttpClient(); client.BaseAddress = new Uri(&quot;http:\/\/localhost:56851\/&quot;);  \/\/ Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(     new MediaTypeWithQualityHeaderValue(&quot;application\/json&quot;));  HttpResponseMessage response = client.GetAsync(&quot;api\/User&quot;).Result;  if (response.IsSuccessStatusCode) {     var users = response.Content.ReadAsAsync&#038;     lt;IEnumerable&lt;Users&gt;&gt;().Result;     usergrid.ItemsSource = users;  } else {     MessageBox.Show(&quot;Error Code&quot; +      response.StatusCode + &quot; : Message - &quot; + response.ReasonPhrase); } <\/code><\/pre>\n<p>  \u041d\u043e \u043a\u0430\u043a \u0436\u0435 \u044d\u0442\u043e \u043c\u0443\u0442\u043e\u0440\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043b\u0435\u0437\u0442\u044c \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0442\u0438\u043f\u044b, \u043a\u043e\u0440\u043e\u0447\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u043e\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code>var resp = GetResponse&lt;SomeController&gt;(c =&gt; gc.SomeAction(new Dto{val = &quot;123&quot;})); <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u044d\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 <s>\u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0438\u0447\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438<\/s> \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-202044","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/202044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=202044"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/202044\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=202044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=202044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=202044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}