How to SetBasePath in ConfigurationBuilder in Core 2.0

How can I set the base path in ConfigurationBuilder in Core 2.0. I have googled and found this question, this from Microsoft docs, and the 2.0 docs online but they seem to be using a version of Microsoft.Extension.Configuration from 1.0.0-beta8. I want to read appsettings.json. Is there a new way of doing this in Core … Read more

Return file in ASP.Net Core Web API

Problem I want to return a file in my ASP.Net Web API Controller, but all my approaches return the HttpResponseMessage as JSON. Code so far public async Task<HttpResponseMessage> DownloadAsync(string id) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent({{__insert_stream_here__}}); response.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/octet-stream”); return response; } When I call this endpoint in my browser, … Read more

Unexpected outcome of node.js vs ASP.NET Core performance test

I am doing a quick stress test on two (kinda) hello world projects written in node.js and asp.net-core. Both of them are running in production mode and without a logger attached to them. The result is astonishing! ASP.NET core is outperforming node.js app even after doing some extra work whereas the node.js app is just … Read more

.NET Core DI, ways of passing parameters to constructor

Having the following service constructor public class Service : IService { public Service(IOtherService service1, IAnotherOne service2, string arg) { } } What are the choices of passing the parameters using .NET Core IOC mechanism services.AddSingleton<IOtherService , OtherService>(); services.AddSingleton<IAnotherOne , AnotherOne>(); services.AddSingleton<IService>(x => new Service( services.BuildServiceProvider().GetService<IOtherService>(), services.BuildServiceProvider().GetService<IAnotherOne >(), “”)); Is there any other way ? 4 … Read more

How to unit test with ILogger in ASP.NET Core

This is my controller: public class BlogController : Controller { private IDAO<Blog> _blogDAO; private readonly ILogger<BlogController> _logger; public BlogController(ILogger<BlogController> logger, IDAO<Blog> blogDAO) { this._blogDAO = blogDAO; this._logger = logger; } public IActionResult Index() { var blogs = this._blogDAO.GetMany(); this._logger.LogInformation(“Index page say hello”, new object[0]); return View(blogs); } } As you can see I have 2 … Read more

InvalidOperationException: Unable to resolve service for type ‘Microsoft.AspNetCore.Http.IHttpContextAccessor’

I started to convert my asp.net core RC1 project to RC2 and faced with problem that now IHttpContextAccessordoes not resolved. For sake of simplicity I created new ASP.NET RC2 project using Visual Studio Template ASP.NET Core Web Application (.Net Framework). Than I added constructor for HomeController which template created for me. public HomeController(IHttpContextAccessor accessor) { … Read more

ASP.NET Core return JSON with status code

I’m looking for the correct way to return JSON with a HTTP status code in my .NET Core Web API controller. I use to use it like this: public IHttpActionResult GetResourceData() { return this.Content(HttpStatusCode.OK, new { response = “Hello”}); } This was in a 4.6 MVC application but now with .NET Core I don’t seem … Read more