IServiceCollection Does not contain definition AddJsEngineSwitcher - c#

Trying to build ReactJS.NET in Visual Studio 2015
using Microsoft.AspNetCore.Http;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.ChakraCore;
using React.AspNet;
In ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddReact();
services.AddJsEngineSwitcher(options => options.DefaultEngineName = ChakraCoreJsEngine.EngineName)
.AddChakraCore();
services.AddMvc();
}
Got an Error to this part:
AddJsEngineSwitcher
IServiceCollection does not contain definition for AddJsEngineSwitcher
Trying to fix some solutions but got no luck.
Checking also my Reference
JavaScriptEngineSwitcher 3.0.0 is exists.
Also searching in Nuget package and changed JavaScriptEngineSwitcher but still the same.
I am using .NET Framework 4.6.

The docs show that you need to install this NuGet package, which contains the AddJsEngineSwitcher extension method that's missing:
JavaScriptEngineSwitcher.Extensions.MsDependencyInjection
Once installed, you'll also need to add the following using to include the namespace:
using JavaScriptEngineSwitcher.Extensions.MsDependencyInjection;

Related

How to write program.cs file for .NET 6 Library Project? - getting error "Program using top-level statements must be an executable"

I keep all the data related classes, interfaces, configurations, etc in a separate library project so it can easily be reused for any other project I need (API, WebAssembly, Mobile, Server Pages, etc..)
I have converted all the projects from the solution to .NET6. They all work and build, as the did before, except for the data library which is giving me the following error: Program using top-level statements must be an executable.
How do I write the new .NET 6 Program.cs file for a .NET 6 library?
The new Program.cs file that is not working:
using FlasherData.Context;
using FlasherData.Repositories;
using FlasherData.Repositories.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<FlasherContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("FlasherDb")));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
var app = builder.Build();
app.Run();
The old .NET 5 Startup.cs file that worked:
using FlasherData.Context;
using FlasherData.Repositories;
using FlasherData.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FlasherData
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// SQLite connection and database context
services.AddDbContext<FlasherContext>(options => options.UseSqlite(Configuration.GetConnectionString("FlasherDb")));
// Dependency Injection
services.AddScoped<IUnitOfWork, UnitOfWork>();
}
}
}
The .NET 5 library did not have a Program.cs file as it was not needed in a .NET 5 library project.
This is a link to the entire solution: Flasher
The first example you show isn't library code, but the entry point of an application. Top-level-statements are internally transformed into the Main method of the assembly, it's really just a syntactical alternative. But a library must not have a Main method, and that's why you get the mentioned error.
Therefore, for a library, either just remove the startup code or (to get the equivalent behavior than before) change it back to what it was before. There's no need to use top-level-statements anywhere. When updating an application from .NET 5.0 to .NET 6.0, typically you don't have to change anything (except in the project files), because the existing code base is fully backwards compatible.

logging.AddAzureWebAppDiagnostics() does not work in .net core 2.2

I've updated my project from .net core 2.1 to 2.2 and then logging.AddAzureWebAppDiagnostics() in Program.cs no longer works.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddAzureWebAppDiagnostics();
})
.UseStartup<Startup>()
.Build();
}
'ILoggingBuilder' does not contain a definition for 'AddAzureWebAppDiagnostics' and no accessible extension method 'AddAzureWebAppDiagnostics' accepting a first argument of type 'ILoggingBuilder' could be found (are you missing a using directive or an assembly reference?
Referring to this document,
If targeting .NET Framework or referencing the Microsoft.AspNetCore.App metapackage, add the provider package to the project. Invoke AddAzureWebAppDiagnostics on an ILoggerFactory instance:
So the way might be slightly different from the previous one. How do I fix this issue?
The documentation is a bit tricky but if read carefully it become clear that following steps should be undertaken (for NET Core):
Microsoft.Extensions.Logging.AzureAppServices should be installed
There is NO need to call logging.AddAzureWebAppDiagnostics();
Logging can be configured using following code
// file startup.cs
using Microsoft.Extensions.Logging.AzureAppServices;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//...
services.Configure<AzureFileLoggerOptions>(Configuration.GetSection("AzureLogging"));
}
}
File appsettings.json should contain
"AzureLogging": {
"FileName" : "azure-diagnostics-",
"FileSizeLimit": 50024,
"RetainedFileCountLimit": 5
}
Logging should be turned on on Azure Portal. After enabling, Azure Portal may ask for installing addon. Message requiring to install addon will appear on logging config page.
Call logger.LogWarning ("message"); in your code to write to log file. If you use LogWarning be sure to set Level to Warning or more detailed (Info or Debug)

Structure Map method Populate() doesn't work for ASP.NET Core

According to StructureMap documentation and examples from StructureMap.Microsoft.DependencyInjection repository it has to work but it doesn't.
Here is my Startup class:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<IMovieRepository, MovieRepository>();
var container = new Container();
container.Configure(config =>
{
config.AddRegistry(new MyRegistry());
config.Populate(services);
});
return container.GetInstance<IServiceProvider>();
}
And Registry:
public class MyRegistry : Registry
{
public MyRegistry()
{
For<IMovieRepository>().Transient().Use<MovieRepository>();
}
}
And here is error screenshot:
What's wrong with my code?
You should also add the following nuget package to your project in order to use the Populate method of the Configuration option.
The package name: StructureMap.Microsoft.DependencyInjection
You do not have to import this library to the startup class though. "using StructureMap" there handles everything.
I decided to change IoC to Autofac. And the same problem appeared. I was following autofac documentation for asp.net core and skip a little detail. It took three days to figure out that I referenced to the wrong package. I referenced to the autofac package when what I was truly need was Autofac.Extensions.DependencyInjection package. It's ridiculous mistake that kick me off for a three days. I am truly convinced that the same kind of mistake I did with structure map, so just look for StructureMap.AspNetCore package instead of StructureMap package and everything will work.
!Read documentation extremely attentively!

Getting Autofac to work with MVC6/ASP.NET5

I cannot get autofac to work, I have looked at this potentially duplicate question, but it doesn't help.
I am using the full .NET stack, DNX 4.5.1
I have included the following dependencies.
"dependencies": {
// matched latest autofac version with latest dependencyinjection version.
"Autofac": "4.0.0-beta8-157",
"Autofac.Framework.DependencyInjection": "4.0.0-beta8-157",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final" ...
And the following initialisation code.
// void?
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var container = new ContainerBuilder();
...
// compilation error here!
container.Populate(services);
}
I am receiving this error:
Error CS1503 Argument 2: cannot convert
from'Microsoft.Extensions.DependencyInjection.IServiceCollection' to
'System.Collections.Generic.IEnumerable<Microsoft.Framework.DependencyInjection.ServiceDescriptor>'
MuWapp.DNX 4.5.1 C:\MuWapp\Startup.cs 54 Active
For RC1 you will need to use the Autofac.Extensions.DependencyInjection package.
https://www.nuget.org/packages/Autofac.Extensions.DependencyInjection/
We renamed our package to align with Microsoft's rename to Microsoft.Extensions.DependencyInjection. It's been a moving target supporting the early DNX releases.
As I've mentioned in the comment, you should use compatible versions of all packages in your project.json. I see on their page: https://github.com/autofac/Autofac/releases that they have released version for RC1, however there is no Autofac.Framework.DependencyInjection for RC1, so if you require this package, you will be unable to run it.
I think you should use built-in dependency injection during your development untill there is RTM version and all of the third-party packages will become stable.
Build-in DI has functionality for injecting classes into controllers, properties as well as attributes, so unless you use some advanced scenarios in which autofac is necessary, you should stick to asp.net 5 DI.

'Owin.IAppBuilder' does not contain a definition for 'MapSignalR'

Error
'Owin.IAppBuilder' does not contain a definition for 'MapSignalR' and
no extension method 'MapSignalR' accepting a first argument of type
'Owin.IAppBuilder' could be found (are you missing a using directive
or an assembly reference?)
Code
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
Any help would be greatly appreciated...
Update
signalR version 2.0.3
Microsoft Owin version 2.0.2
Owin version 1.0.0
Visual Studio 2012
Only install this nuget:
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
Finally was able to solve it by adding signalR dependencies before adding signalR from NuGet Packages
Step's I followed:
Added Microsoft.Owin //version 2.0.1
Added Microsoft.Owin.Security //version 2.0.1
Added Microsoft Asp.Net SignalR
The reason I discovered was a problem with version 2.0.2 of Microsoft.Owin and Microsoft.Owin.Security and then adding a class named Startup.cs with following code:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(webApp.Startup))]
namespace webApp
{
public static class Startup
{
public static void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Directly adding Microsoft Asp.Net SignalR from NuGet adds version 2.0.2 of Microsoft.Owin and Microsoft.Owin.Security which creates the problem.
Hope it helps someone...!!
Update-Package Owin -Reinstall
worked for me
Install Nuget Package: Microsoft.AspNet.Identity.Owin
I had this same problem. Turns out my .csproj file the first line:
Project ToolsVersion="12.0" didn't match my other files. Changed it to:
Project ToolsVersion="14.0"
and no more compile issue.
Using MVC5, enter your Startup.cs file.
Add IAppBuilder appBuilder to Configure():
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAppBuilder appBuilder)
{
Then, under
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
add appBuilder.MapSignalR();
Asp.Net 5 empty mvc project out of the box creates something that looks like this
using Microsoft.Owin;
using Owin;
namespace DeadlyChat
{
public class Startup
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
app.MapSignalR();
}
}
}
Took me a while to notice that Configure was supposed to be Configuration and IApplicationBuilder needs to be IAppBuilder. I also pulled off the assembly annotation above the namespace.
I wound up scrapping trying to use Asp.Net 5 couldn't get it to work. Went back to 4.6 and everything worked fine. Followed this walkthrough http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

Categories