dimanche 18 février 2018

No response from HttpResponseMessage

I have MVC project with service like below:

namespace comp.Services
{
    public class CompService
    {
        public HttpClient client = new HttpClient();

        public CompService()
        {
            client.BaseAddress = new Uri("someapiurl");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
        }

        protected async Task<string> GetProductAsync(string path)
        {
            var resp = "nothing here";
            HttpResponseMessage response = await client.GetAsync(path);
            if (response.IsSuccessStatusCode)
            {
                resp = await response.Content.ReadAsStringAsync();
            }
            return resp;
        }

        public string GetProduct(string path)
        {
            return GetProductAsync(path).GetAwaiter().GetResult();
        }
    }
}

and actionResult to view:

namespace comp.Controllers
{
    public class HomeController : Controller
    {
        public CompService compService;
        public HomeController()
        {
            compService = new CompService();
        }

        public ActionResult About()
        {
            var timeServer = compService.GetProduct("/api/time");
            ViewBag.timeServer = timeServer;
            return View();
        }
    }
}

When in debuger I encounter this line:

HttpResponseMessage response = await client.GetAsync(path);

program exit from debuger and there is no respone in browser.

The same code written in console application works.

In VS output is message that response is succes:

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2018-02-18T13:48:31","tags":{"ai.internal.sdkVersion":"rddf:2.2.0-738","ai.internal.nodeName":"DESKTOP-xxxxx","ai.cloud.roleInstance":"DESKTOP-xxxxx"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"/api/v1/time","id":"xxxxx=","data":"https://api.xxx.com/api/v1/time","duration":"00:00:01.3150000","resultCode":"200","success":true,"type":"Http","target":"xxx","properties":{"DeveloperMode":"true"}}}}

In browser console output:

[14:51:33 GMT+0100 (Central European Standard Time)] Browser Link: Failed to send message to browser link server:
Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()

Thanks for help.




Aucun commentaire:

Enregistrer un commentaire