All Questions
25,131
questions
2893
votes
13
answers
426k
views
What are the correct version numbers for C#?
What are the correct version numbers for C#? What came out when? Why can't I find any answers about C# 3.5?
This question is primarily to aid those who are searching for an answer using an incorrect ...
2093
votes
14
answers
1.5m
views
AddTransient, AddScoped and AddSingleton Services Differences
I want to implement dependency injection (DI) in ASP.NET Core. So after adding this code to ConfigureServices method, both ways work.
What is the difference between the services.AddTransient and ...
724
votes
31
answers
393k
views
How to check if a number is a power of 2
Today I needed a simple algorithm for checking if a number is a power of 2.
The algorithm needs to be:
Simple
Correct for any ulong value.
I came up with this simple algorithm:
private bool ...
522
votes
16
answers
379k
views
Command dotnet ef not found
I'm following the docs in order to create an initial migration. When I execute dotnet, I get the help section, meaning that the PATH works properly.
Then I try to execute the command below from the ...
458
votes
20
answers
571k
views
How to unapply a migration in ASP.NET Core with EF Core
When I run PM> Remove-Migration -context BloggingContext in VS2015 with an ASP.NET Core project using EF Core, I get the following error:
System.InvalidOperationException: The migration '...
448
votes
29
answers
879k
views
How to read AppSettings values from a .json file in ASP.NET Core
I have set up my AppSettings data in file appsettings/Config .json like this:
{
"AppSettings": {
"token": "1234"
}
}
I have searched online on how to read AppSettings values from .json ...
428
votes
21
answers
529k
views
How to determine if .NET Core is installed
I know that for older versions of .NET, you can determine if a given version is installed by following
https://support.microsoft.com/en-us/kb/318785
Is there an official method of determining if ....
338
votes
8
answers
189k
views
.NET Core vs Mono
What is the difference between .NET Core and Mono?
I found a statement on the official site that said: "Code written for it is also portable across application stacks, such as Mono."
My goal is to ...
305
votes
3
answers
463k
views
How to get HttpContext.Current in ASP.NET Core? [duplicate]
We are currently rewriting/converting our ASP.NET WebForms application using ASP.NET Core. Trying to avoid re-engineering as much as possible.
There is a section where we use HttpContext in a class ...
291
votes
10
answers
961k
views
How to remove item from list in C#?
I have a list stored in resultlist as follows:
var resultlist = results.ToList();
It looks something like this:
ID FirstName LastName
-- --------- --------
1 Bill Smith
2 John ...
283
votes
15
answers
483k
views
How to read values from the querystring with ASP.NET Core?
I'm building one RESTful API using ASP.NET Core MVC and I want to use querystring parameters to specify filtering and paging on a resource that returns a collection.
In that case, I need to read the ...
275
votes
11
answers
217k
views
Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing
I had an Asp.Net core 2.2 project.
Recently, I changed the version from .net core 2.2 to .net core 3.0 Preview 8. After this change I see this warning message:
using 'UseMvc' to configure MVC is ...
271
votes
15
answers
228k
views
Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable
I have a simple class like this.
public class Greeting
{
public string From { get; set; }
public string To { get; set; }
public string Message { get; set; }
}
Strangely I get the ...
269
votes
4
answers
246k
views
.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 ...
241
votes
28
answers
104k
views
Unable to find testhost.dll. Please publish your test project and retry
I have a simple dotnet core class library with a single XUnit test method:
TestLib.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0&...
238
votes
14
answers
381k
views
Automatically set appsettings.json for dev and release environments in asp.net core?
I've defined some values in my appsettings.json for things like database connection strings, webapi locations and the like which are different for development, staging and live environments.
Is there ...
229
votes
6
answers
347k
views
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> ...
214
votes
12
answers
93k
views
Should I take ILogger, ILogger<T>, ILoggerFactory or ILoggerProvider for a library?
This may be somewhat related to Pass ILogger or ILoggerFactory to constructors in AspNet Core?, however this is specifically about Library Design, not about how the actual application that uses those ...
213
votes
7
answers
182k
views
Is ConfigurationManager.AppSettings available in .NET Core 2.0?
I've got a method that reads settings from my config file like this:
var value = ConfigurationManager.AppSettings[key];
It compiles fine when targeting .NET Standard 2.0 only.
Now I need multiple ...
205
votes
5
answers
145k
views
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 ...
194
votes
9
answers
182k
views
Copy files to output directory using csproj dotnetcore
So my issue is pretty simple. I have some files that I want to be copied to the build output directory whether it is a debug build or a release publish. All of the information I can find is about the ...
192
votes
11
answers
191k
views
Unable to resolve ILogger from Microsoft.Extensions.Logging
I've configured my console application's Main like so
var services = new ServiceCollection()
.AddLogging(logging => logging.AddConsole())
.BuildServiceProvider();
And then I try to use it ...
191
votes
3
answers
94k
views
Determine Operating System in .NET Core
How can I determine which operating system my .NET Core app is running on?
In the past I could use Environment.OSVersion.
What is the current way to determine whether my app is running on Mac or ...
185
votes
6
answers
248k
views
Send HTTP POST message in ASP.NET Core using HttpClient PostAsJsonAsync
I want to send dynamic object like
new { x = 1, y = 2 };
as body of HTTP POST message. So I try to write
var client = new HttpClient();
but I can't find method
client.PostAsJsonAsync()
So I ...
183
votes
7
answers
193k
views
How to auto create database on first run?
My application being ported to .NET Core will use EF Core with SQLite. I want to automatically create the database and tables when the app is first run. According to documentation this is done using ...
178
votes
54
answers
444k
views
HTTP Error 500.30 - ANCM In-Process Start Failure
I was experimenting with a new feature that comes with .NET core sdk 2.2 that is supposedly meant to improve performance by around 400%.
Impressive so I tried it out on my ABP (ASP.NET Boilerplate) ...
178
votes
11
answers
202k
views
How to log to a file without using third party logger in .Net Core?
How to log to a file without using third party logger (serilog, elmah etc.) in .NET CORE?
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
}
public void ...
175
votes
6
answers
110k
views
How do I get .NET Core projects to copy NuGet references to the build output?
I'm trying to write a plugin system with .NET Core, and one of my requirements are to be able to distribute the plugin DLL along with its dependencies to the user for install.
However, I can't figure ...
175
votes
6
answers
106k
views
Pros and cons of AppSettings vs applicationSettings (.NET app.config / Web.config)
When developing a .NET Windows Forms Application we have the choice between those App.config tags to store our configuration values. Which one is better?
<configuration>
<!-- Choice 1 --&...
173
votes
11
answers
178k
views
ASP.NET Core configuration for .NET Core console application
ASP.NET Core support a new configuration system as seen here:
https://docs.asp.net/en/latest/fundamentals/configuration.html
Is this model also supported in .NET Core console applications?
If not ...
161
votes
15
answers
238k
views
How to use appsettings.json in Asp.net core 6 Program.cs file
I'm trying to access appsettings.json in my Asp.net core v6 application Program.cs file, but in this version of .Net the Startup class and Program class are merged together and the using and another ...
156
votes
2
answers
8k
views
If (false == true) executes block when throwing exception is inside
I have a rather strange problem that is occurring.
This is my code:
private async Task BreakExpectedLogic()
{
bool test = false;
if (test == true)
{
Console.WriteLine("Hello!");
...
148
votes
12
answers
184k
views
Setting the version number for .NET Core projects - CSPROJ - not JSON projects
This question is very similar to Setting the version number for .NET Core projects, but not the same. Using the latest stable version of .NET Core at the time of writing (1.1) and VS2017, .NET Core ...
148
votes
21
answers
104k
views
Auto Versioning in Visual Studio 2017 (.NET Core)
I have spent the better part of a few hours trying to find a way to auto-increment versions in a .NETCoreApp 1.1 (Visual Studio 2017).
I know the the AssemblyInfo.cs is being created dynamically in ...
144
votes
17
answers
229k
views
Entity Framework Core: `SqlNullValueException: Data is Null.` How to troubleshoot?
I am using Entity Framework Core in an ASP.NET Core application and Controller action and I haven't changed something to the working code nor to the database but I can't tell what is the query ...
144
votes
5
answers
68k
views
How to consume a Scoped service from a Singleton?
How should I inject (using .NET Core's built-in dependency injection library, MS.DI) a DbContext instance into a Singleton? In my specific case the singleton is an IHostedService?
What have I tried
I ...
141
votes
11
answers
94k
views
How to mock IConfiguration.GetValue
I tried in vain to mock a top-level (not part of any section) configuration value (.NET Core's IConfiguration). For example, neither of these will work (using NSubstitute, but it would be the same ...
140
votes
14
answers
138k
views
What replaces WCF in .Net Core?
I am used to creating a .Net Framework console application and exposing a Add(int x, int y) function via a WCF service from scratch with Class Library (.Net Framework). I then use the console ...
139
votes
6
answers
128k
views
How to extract a list from appsettings.json in .net core
I have an appsettings.json file which looks like this:
{
"someSetting": {
"subSettings": [
"one",
"two",
"three"
]
}
}
When I build my ...
138
votes
5
answers
231k
views
Where is NuGet.Config file located in Visual Studio project?
I am wondering where is NuGet.Config file located in Visual Studio 2017 project? I tried to create my own NuGet.Config file in the root of the project, but I didn't find any new repositories (NuGet ...
135
votes
3
answers
75k
views
How to call ThenInclude twice in EF Core?
I'm creating an ASP.NET Core API app, and relying on EF Core. I have entities defined like this:
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string ...
132
votes
11
answers
117k
views
Upload files and JSON in ASP.NET Core Web API
How can I upload a list of files (images) and json data to ASP.NET Core Web API controller using multipart upload?
I can successfully receive a list of files, uploaded with multipart/form-data ...
130
votes
3
answers
49k
views
Are async console applications supported in .NET Core?
At some point in time the CoreCLR supported async main entry points. See http://blog.stephencleary.com/2015/03/async-console-apps-on-net-coreclr.html
However both the following programs are not ...
128
votes
26
answers
296k
views
How to change the port number for Asp.Net core app?
I added the following section in project.json.
"commands": {
"run": "run server.urls=http://localhost:8082",
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --...
127
votes
24
answers
175k
views
MSBUILD throws error: The SDK 'Microsoft.NET.Sdk' specified could not be found
I'm trying to build a solution using msbuild command line and I keep getting this error:
error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found.
The version of msbuild is the latest ...
126
votes
7
answers
133k
views
Switch between dotnet core SDK versions
I recently installed VS 2017 RC and then automatically my dotnet version pointed to 1.0.0-preview4-004233. Due to that whenever I create a new project using command dotnet new -t Console I cannot see ...
126
votes
4
answers
104k
views
.NET Core IServiceScopeFactory.CreateScope() vs IServiceProvider.CreateScope() extension
My understanding is that when using the built in the dependency injection, a .NET Core console app will require you to create and manage all scopes yourself whereas a ASP.NET Core app will create and ...
125
votes
14
answers
122k
views
How to load appsetting.json section into Dictionary in .NET Core?
I am familiar with loading an appsettings.json section into a strongly typed object in .NET Core Startup.cs. For example:
public class CustomSection
{
public int A {get;set;}
public int B {get;...
123
votes
3
answers
73k
views
'ConfigurationBuilder' does not contain a definition for 'AddJsonFile'
I have the following error:
Program.cs(15,72): error CS1061: 'ConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no accessible extension method 'AddJsonFile' accepting a first ...
122
votes
6
answers
168k
views
How to set json serializer settings in asp.net core 3?
json serializer settings for legacy asp.net core applications were set by adding AddMvc().AddJsonOptions(), but I don't use AddMvc() in asp.net core 3. So how can I set global json serialization ...