I have .NET Core project in Visual Studio 2017. I am trying to add (Postgresql) database connection. Here is a code:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<ConexionWebApi>(options => {
options.UseNpgsql("ConnectionString", b => b.MigrationsAssembly("WebAPISample"));
});
}
But useNpgsql generates the following error:
'DbContextOptionsBuilder' does not contain a definition for 'UseNpgsql' and no extension method 'UseNpgsl' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly refence?)
I installed the followings NuGet packages:
Microsoft.EntityFrameworkCore.Tools,
Npgsql.EntityFrameworkCore.PostgreSQL,
Npgsql.EntityFrameworkCore.PostgreSQL.Design.
Should I install some other library?
I had the same issue. I resolved it by adding
using Microsoft.EntityFrameworkCore;
Related
I used to use .NET core 5, but now I'm trying to use .NET core 6, it seems the old Startup.cs is mixed now with Program.cs.
The thing is that I'm trying to add log4net to my project, and it gives me an error I'm not sure what is happening or I'm missing something. The error says:
Severity Code Description Project File Line Suppression State
Error CS1061 'ILoggingBuilder' does not contain a definition for 'AddLog4Net' and no accessible extension method 'AddLog4Net' accepting a first argument of type 'ILoggingBuilder' could be found
I have installed log4net via Nuget Managment and Package Manager Console Nothing seems to work, here is a part of my Program.cs file:
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddLog4Net(); // <-- here is where I got the error
// Add services to the container.
var services = builder.Services;
services.AddCors();
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddLogging();
You need a separate extension package to make it work in most use cases.
The package you need is:
https://www.nuget.org/packages/Microsoft.Extensions.Logging.Log4Net.AspNetCore/
Documentation and source:
https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
You can use similar like below:
builder.Host.ConfigureLogging(logging =>
{
logging.AddLog4Net(log4NetConfigFile: "log4net.config");
logging.ClearProviders();
logging.AddConsole();//for Logging on Console
logging.AddLog4Net();//for DB Query Logging
});
I created an ASP.NET Core web application and I tried to add the following code for implementing authentication but the AddSignIn method could not be found.
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddSignIn("AzureAdB2C", Configuration, // This method is missing
options => Configuration.Bind("AzureAdB2C", options));
I am using the Microsoft.AspNetCore.Authentication.OpenIdConnect package so I'm not sure what I'm missing here.
AddSignIn is in the namespace Microsoft.Identity.Web, which is in the package of the same name.
Have you installed the package? https://www.nuget.org/packages/Microsoft.Identity.Web
I inherited a bit of code:
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
I think is stems from a .net Core application. First I struggled with this bit (in the sense that I could not compile it in .Net Framework):
.SetBasePath(Directory.GetCurrentDirectory())
But I came across this. The accepted answer solved it. Now I am struggling with this bit (again in the sense that I cannot compile it in .Net Framework)::
.AddJsonFile("appsettings.json")
Is there a way to fix this please (usually I would get such data from App.Config ...)? Thanks.
PS:
More code plus error message:
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration;
namespace SandboxSecurityToken
{
class Program
{
static void Main(string[] args)
{
...
static async Task RunAsync()
{
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Severity Code Description Project File Line Suppression State
Error CS1061 'IConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no accessible extension method 'AddJsonFile' accepting a first argument of type 'IConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)
This is .NET Standard code that should work just fine on .NET Framework 4.6.1 or higher.
You don't provide enough information, but I would guess you are missing reference to this NuGet package : https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/2.2.0
Or you might be missing proper using:
using Microsoft.Extensions.Configuration;
So that AddJsonFile extension method is found.
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 argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly
The project is a dotnet core app, console application, using Azure Search SDK
The line of error is below:
using System;
using System.Linq;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Spatial;
namespace DemoSearchIndexer
{
class Program
{
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
...
The AddJsonFile extension method is available in NuGet:
Microsoft.Extensions.Configuration.Json
When building an ASP.NET Core application, which usually references Microsoft.AspNetCore.App (or, historically, Microsoft.AspNetCore.All), you get this "for free".
When building a console application, or something that doesn't reference the metapackages, you need an explicit reference to Microsoft.Extensions.Configuration.Json.
You also have to set appsettings file properties to "copy always".
See this for more infos.
I want to build a self Host of a Web Api 2 Webservice.
I am using the .Net Core 1.0 Framework for my Console App and also for the Web Api 2 Project.
Since there was a name change for the packages and Owin is deprecated, I am not able to rebuild the given samples in the official GitHub repository of ASPnet:
https://github.com/aspnet/Hosting/blob/dev/samples/SampleStartups/StartupHelloWorld.cs
(I tried with this one)
I looked in the project.json file which packages they are using but since I can´t find the AspNetCore.Hosting package only a package named AspNet.Hosting but i isn´t working with both packages.
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNetCore.Hosting": "1.0.0-*",
"Microsoft.NETCore.Platforms": "1.0.1-*"
},
Resharper says something like :
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'WebHostBuilder' could not be found (are you missing a using directive or an assembly reference?) WebApiConsoleHost.DNX 4.5.1, WebApiConsoleHost.DNX Core 5.0
Just to be complete, I used the whole Main Function Body of the given sample in the Link:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseDefaultConfiguration(args)
.UseStartup<StartupHelloWorld>()
.Build();
host.Run();
}
Do someone know where the WebHostBuilder class is defined ? Resharper suggests that it is in AspNet.Hosting, but after adding the using it is still not found!
Try to read the latest docs. It is working for me.