asp.net-core-2.1

Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

本秂侑毒 提交于 2019-11-28 04:29:48
When creating test projects or upgrading an application and tests to ASP.NET Core 2.1 / .NET Core 2.1, running tests fails with assembly load exceptions like System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. When adding references to some other libraries there are also build warnings like warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Options" that could not be resolved. warning MSB3277: Found conflicts

JWT cannot be retrieved by HttpContext.GetTokenAsync in .NET Core 2.1

给你一囗甜甜゛ 提交于 2019-11-28 04:09:41
问题 This one really has me scratching my head as I can create a JWT. I can add an attribute to authorize a controller and see if I do not add an 'Authorization' 'Bearer (token)' to a header it will return a 401 unauthorized. However something as simple as getting the string of the token to get it's payload claims is not working. So this works fine: var token = Request.Headers["Authorization"]; This does not: var token2 = await HttpContext.GetTokenAsync(JwtBearerDefaults.AuthenticationScheme,

Proper way to register HostedService in ASP.NET Core. AddHostedService vs AddSingleton

若如初见. 提交于 2019-11-28 02:32:59
问题 What is proper way to register custom hosted service in ASP.NET Core 2.1? For example I have custom hosted service derived from BackgroundService named MyHostedService . How should I register it? public IServiceProvider ConfigureServices(IServiceCollection services) { //... services.AddSingleton<IHostedService, MyHostedService>(); } or public IServiceProvider ConfigureServices(IServiceCollection services) { //... services.AddHostedService<MyHostedService>(); } ? Here we can see first case,

JsonResult return Json in ASP.NET CORE 2.1

烈酒焚心 提交于 2019-11-28 02:30:17
问题 Controller that worked in ASP.NET Core 2.0: [Produces("application/json")] [Route("api/[controller]")] [ApiController] public class GraficResourcesApiController : ControllerBase { private readonly ApplicationDbContext _context; public GraficResourcesApiController(ApplicationDbContext context) { _context = context; } [HttpGet] public JsonResult GetGrafic(int ResourceId) { var sheduling = new List<Sheduling>(); var events = from e in _context.Grafic.Where(c=>c.ResourceId == ResourceId) select

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

一笑奈何 提交于 2019-11-28 01:18:31
.NET Core 2.1 comes with this new factory called HTTPClientFactory , but I can't figure out how to mock it to unit test some methods that include REST service calls. The factory is being injected using .NET Core IoC container, and what the method does is create a new client from the factory: var client = _httpClientFactory.CreateClient(); And then using the client to get data from a REST service: var result = await client.GetStringAsync(url); The HttpClientFactory is derived from IHttpClientFactory Interface So it is just a matter of creating a mock of the interface var mockFactory = new Mock

Routing is not working with self-hosted web API [duplicate]

江枫思渺然 提交于 2019-11-27 22:27:42
This question already has an answer here: WebApi giving 404 whilst debugging; works when published 1 answer This is essentially what I have, a very simple set of three files with fresh asp.net core 2.1 (actually copy-pasted from tutorials): public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); } Then goes the simplest startup class Startup { public Startup(IConfiguration configuration) { Configuration =

Should HttpClient instances created by HttpClientFactory be disposed?

我的未来我决定 提交于 2019-11-27 17:47:31
问题 So, I've registered a named client with the services collection in my Startup.cs: services.AddHttpClient(someServiceName, client => client.BaseAddress = baseAddress); and now can inject an IHttpClientFactory from my service provider. Using this IHttpClientFactory , I conjure up a client instance: var client = httpClientFactory.CreateClient(someServiceName) Once upon a time, it was necessary to be very careful about the disposing of HttpClient instances, as it was rarely the right thing to do.

How to specify the view location in asp.net core mvc when using custom locations?

老子叫甜甜 提交于 2019-11-27 03:27:57
Let's say I have a controller that uses attribute based routing to handle a requested url of /admin/product like so: [Route("admin/[controller]")] public class ProductController: Controller { // GET: /admin/product [Route("")] public IActionResult Index() { return View(); } } Now let's say that I'd like to keep my views organized in a folder structure that roughly reflects the url paths they are related to. So I'd like the view for this controller to be located here: /Views/Admin/Product.cshtml To go further, if I had a controller like this: [Route("admin/marketing/[controller]")] public class

Is .NET Core Runtime backwards compatible with previous releases?

白昼怎懂夜的黑 提交于 2019-11-27 03:24:36
问题 If I've installed the latest version of the .NET Core Runtime (as of now, that's version 2.2.3): https://dotnet.microsoft.com/download/dotnet-core/2.2 Is that one installation backwards-compatible with previous releases of .NET Core? For example, will the above installation be compatible with an app which targets: netcoreapp1.0 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 I realize that the .NET runtimes can be installed side-by-side. However, that does not answer my question. Is the one 2.2

Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

自古美人都是妖i 提交于 2019-11-27 03:22:22
Does any one know how I can specify the Default value for a DateTime property using the System.ComponentModel DefaultValue Attribute? for example I try this: [DefaultValue(typeof(DateTime),DateTime.Now.ToString("yyyy-MM-dd"))] public DateTime DateCreated { get; set; } And it expects the value to be a constant expression. This is in the context of using with ASP.NET Dynamic Data. I do not want to scaffold the DateCreated column but simply supply the DateTime.Now if it is not present. I am using the Entity Framework as my Data Layer Cheers, Andrew You cannot do this with an attribute because