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
});
Related
(https://i.stack.imgur.com/J4v7h.png)
I have installed both AutoMapper and AutoMapper.Extensions.Microsoft.DependencyInjection
But still I am getting the same error
I didn't add the builder.services line in program.cs file that is
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
Have you added it in Program.cs like
builder.Services.AddAutoMapper(config =>
{
config.AddProfile(typeof(YourMappingProfile));
});
I'm rebuilding a project of mine from .NET Framework to .NET Core and was curious to see if I'm able to "plug" in my Angular JS front-end project into the new .NET Core project using SpaServices. I've found some tutorials and this is what I have (my Angular folder is in the same project as my Startup.cs and API):
In ConfigureServices() in Startup.cs:
services.AddSpaStaticFiles(config => {
config.RootPath = "/MyAngularProject";
});
In Configure() in Startup.cs:
app.UseSpa(spa => {
spa.Options.SourcePath = "/MyAngularProject";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
The Angular folder is not named 'ClientApp'. When I run my backend and then the Angular project in VS Code, I get this error:
AggregateException: One or more errors occurred. (One or more errors occurred. (Failed to start 'npm'. To resolve this:.
[1] Ensure that 'npm' is installed and can be found in one of the PATH directories.
There is also an Inner Exception:
[2] See the InnerException for further details of the cause.
---> System.ComponentModel.Win32Exception (267): The directory name is invalid.
I've looked and made sure my PATH variable is set to the node.js folder and the error is still being thrown. I don't think I'm pointing at the wrong directory either for my Angular project. I do have the package.json file that holds the start script in a folder within MyAngularProject. I've also tried navigating into that folder but it produces the same errors. Can anyone help point me in the right direction? Is this even possible given the project is AngularJS? Thank you for any help!
UPDATE:
Here is the folder structure:
Solution 'MyProject'
MyProject.API
Controllers (folder)
MyAngularProject (folder)
app (folder)
[npm script is located in here]
Startup.cs
Program.cs
appsettings.json
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'm developing an Azure webjob using .NET Framework but when I'll run this localy, I've this exception after the startup.
Microsoft.Azure.WebJobs.Host.Listeners.FunctionListenerException: The listener for function [Name of function] was unable to start.
Inner exception:
ArgumentNullException: Value cannot be null.
Parameter name: connectionString
This is the code inside the Program class of the web job.
static void Main()
{
HostBuilder builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddTimers();
});
using (IHost host = builder.Build())
{
host.Run(); // <-- error happens on this line
}
}
Inside the App.config I've added next two connection strings:
<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key]" />
<add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key]" />
With [Name] and [Key] as the account name en key from a live environment.
Also when I change the connection strings to UseDevelopmentStorage=true, I'm getting the same exception.
How could I solve this?
Update:
If you're using Azure WebJobs SDK of version 3.x with .NET Framework, there is an issue: Version 3.x is incompatible with .NET Framework and PackageReference.
So there're several ways as workaround:
Directly use the Azure WebJobs template from Visual studio, it's based on .net framework.
You can add an appsettings.json file (Note: right click the file → select Properties → then set "Copy to Output Directory" to "Copy if newer") to the .NET Framework project, it will work(based on 1, it will throw errors, but it can work). The JSON file looks like below:
{
"AzureWebJobsStorage": "{storage connection string}"
}
The best solution is to use .NET Core with WebJobs SDK of version 3.x.
Original answer:
If you're using WebJob SDK version 3.x, I suggest you should create a .NET Core console project instead of .NET Framework console project, then you should follow this official doc.
First, create a .NET Core console app. And install all the necessary packages mentioned in the official doc.
Your program.cs:
static void Main()
{
HostBuilder builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
using (IHost host = builder.Build())
{
host.Run();
}
}
Then create a function, please refer to this section.
Then add an appsettings.json file (Note: right click the file → select Properties → then set "Copy to Output Directory" to "Copy if newer"), add the AzureWebJobsStorage inside it:
{
"AzureWebJobsStorage": "{storage connection string}"
}
Please let me know if you still have more issues about that.
I want to create token with IdentityServer4 but when I add this line :
services
.AddIdentityServer()
.AddDeveloperSigningCredential() <------ EXCEPTION
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
in my project I have this exception
System.MissingMethodException: 'Method not found: 'System.String IdentityModel.CryptoRandom.CreateUniqueId(Int32)'.'
I add to have the line .AddDeveloperSigningCredential() to my project because previously when i run my api to see my token I have this error :
"Keyset is missing"
When I wanted to run my api.
I follow this to create my project.
I ran into this problem, and this was the cause:
If you are running IdentityServer4, version 2.5.4 or lower, and you reference a project that contains IdentityModel, version 4.0.0 or higher, you will get this error.
Hope this helps someone.
Updating IdentityServer works in my case. You find lastest version here.
Did you do like this?
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()....
And u need to restart the client every time you try to test it.
The client makes a call only once per session