'ConfigurationBuilder' does not contain a definition for 'AddJsonFile' - c#

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.

Related

WPF WebHostBuilder does not contain definition for UseKestrel

I am trying to implement single sign on using OIDC as per GitHub but I keep getting the error:
'WebHostBuilder' does not contain a definition for 'UseKestrel' and no accessible extension method 'UseKestrel' accepting a first argument of type 'WebHostBuilder' could be found (are you missing a using directive or an assembly reference?)
when implementing the SystemBrowser.cs code
using IdentityModel.OidcClient.Browser;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//other code
//simplified code extract below
IWebHost _host = new WebHostBuilder()
.UseKestrel()
.UseUrls(_url)
.Configure(Configure)
.Build();
//other code
According to this it should be part of the Microsoft.AspNetCore.Hosting?
Is there something I'm missing or a NuGet package I need installing? The packages installed so far are:
Any assistance would be appreciated.
The default asp.net core application could work well because the Microsoft.AspNetCore.Hosting package and other related package integrated in the framework.
I test a console application with asp.net core and reproduce your issue.Install the Microsoft.AspNetCore.Hosting together with Microsoft.AspNetCore.Server.Kestrel.
You could download on NuGet Package Manager by searching for the package name or download on Package Manager Console by using the command in the following reference:
https://www.nuget.org/packages/Microsoft.AspNetCore.Server.Kestrel/2.2.0?_src=template

getting security .Net Core code working in .Net Framework Console application

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.

Error CS0006: Metadata file 'MongoDB' could not be found

I have created a Azure Function and it has below code in `run.csx'
using System;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
using MongoDB.Bson.IO;
using MongoDB.Bson;
using MongoDB;
using MongoDB.Driver;
using System.Security.Authentication;
using System.Text;
using Newtonsoft.Json;
public static void Run(string myIoTHubMessage, ILogger log)
{
log.LogInformation($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
}
I am having Project.json as follow
{
"frameworks": {
"net46":{
"dependencies": {
"Newtonsoft.Json": "10.0.3",
"System.ServiceModel.Primitives":"4.4.0",
"MongoDB.Bson": "2.4.0",
"MongoDB.Driver": "2.4.0",
"MongoDB.Driver.Core": "2.4.0"
}
}
}
}
I am getting below error while running the azure function
2019-01-11T10:01:14.846 [Error] run.csx(5,27): error CS0234: The type or namespace name 'Description' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?)
2019-01-11T10:01:15.108 [Error] run.csx(6,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
I even tried adding namespace like below but no luck
#r "Newtonsoft.Json"
#r "System.Xml"
#r "System.Xml.Linq"
#r "MongoDB"
It's probably caused by the difference of Function runtime.
project.json is used for functions on ~1 runtime where code targets at .NET Framework, while the function you create is on ~2 runtime which runs on .NET Core env. When we create a new Function app its runtime is set to ~2 by default now.
So the solution is simple, delete existing functions in the Function app and change Function runtime version to ~1(Find it in portal, Platform features>Function app settings). Then you could recreate an IoT Hub (Event Hub) trigger with steps above, things should work this time.
To work with Function 2.0, use function.proj to install packages.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="<packageName>" Version="<version>"/>
</ItemGroup>
</Project>

RestSharp library not found when running csc

I have this code:
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient
{
BaseUrl = new Uri("www.newuri.com"),
Authenticator = new HttpBasicAuthenticator(username: "myusername", password: "mypassword")
};
var request = new RestRequest(Method.POST);
var response = client.Execute(request);
}
}
}
I'm coding using VS2017 and this program compiles perfectly on it. The problem is that I want to compile and run it via Command Prompt and when I try to run command:
csc Program.cs
I'm getting error:
Program.cs(3,7): error CS0246: The type or namespace name 'RestSharp' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(4,7): error CS0246: The type or namespace name 'RestSharp' could not be found (are you missing a using directive or an assembly reference?)
Am I missing something when compiling via CMD?
If you compile with csc, you will need to manually specify the paths to all your references.
Few people compile by manually invoking csc. Instead, they use build software which understands the csproj file format and runs csc for them.
Can you try running msbuild or dotnet build? That will respect all your references you've added in Visual Studio.

UseNpgsql not available in IServiceCollection in .NET Core

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;

Categories

Resources