Nancy Rendering Embedded views with .Net Core - asp.net-core

I've jumped many hurdles, but alas I still see the default 404 iamge rendered by Nancy.
I've 'mastered' project.json so I can embed my assets (including /assets/views/index.html), using the following block in the project.json file:
"buildOptions": {
"embed": [
"assets/**",
"Content/**",
"fonts/**",
"Scripts/bootstrap.js",
"Scripts/jquery-2.2.2.js",
"Scripts/mustache.js",
"favicon.ico"
//, "Scripts/**" - include this and build fails with errors in *.targets (!)
]
}
This is confirmed when debugging and querying assembly resources. All good.
My Bootstapper contains:
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
DependencyResolver.ConfigureDependencies(container);
ResourceViewLocationProvider
.RootNamespaces
.Add(GetAssembly(), "<name to assembly>"); //embedded resource directory location
}
And yet in my IndexModule:
public class IndexModule : NancyModule
{
public IndexModule()
{
Get("/", args => Response.AsFile("assets/views/index.html", "text/html"));
}
}
This renders nothing... could I be missing anything ? It was working fine with .Net Framework

Related

Razor pages not update until restart project

I add Volo.Account module with source code to my solution for update some functionality of Login/Register. When I update page (like Login.cshtml) changes not shown until restart project.
According to Microsoft doc, I instal Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package and line below to ConfigureServices of {PROJECT}AuthServerModule, but RuntimeCompilation not working.
context.Services.AddRazorPages()
.AddRazorRuntimeCompilation();
actually you don't need to use AddRazorRuntimeCompilation in your application. You can get the advantage of ABP's Virtual File System and configure the AbpVirtualFileSystemOptions in your application module class:
public override void ConfigureServices(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();
var configuration = context.Services.GetConfiguration();
//other configurations...
if (hostingEnvironment.IsDevelopment())
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.ReplaceEmbeddedByPhysical<AbpAccountWebModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("<web-module-project-path>")));
});
}
}
You just need to use the ReplaceEmbeddedByPhysical method. Check the following links for more info:
https://docs.abp.io/en/abp/latest/Virtual-File-System#dealing-with-embedded-files-during-development
https://github.com/abpframework/abp/blob/dev/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs#L148

app does not pick up blazorclasslib pages

i'm using a razor class library contains my _Host.cshtml view :
-WebApp
-Program.cs
-Startup.cs
-MyRazorClassLib
-Pages
-_Host.cshtml
and adding app parts in my webapp as follow :
services.AddRazorPages()
.ConfigureApplicationPartManager(p =>
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
p.ApplicationParts.Add(new AssemblyPart(assembly));
}
});
the razorclasslib assembly is added to the app parts but i'm having :
Cannot find the fallback endpoint specified by route values: { page: /_Host, area: }.
I'am missing/messing something ?
thanks in advance.
Seems yes and documented in Discovery in Application Parts
The applicationName setting needs to be set to the root assembly used for discovery. The root assembly used for discovery is normally the entry point assembly.
for example :
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
/* set the name of the assembly containing the _Host.cshtml */
env.ApplicationName = "myAppName";
}

EF Core 3: CLI "Migrations add" fails on "No database provider has been configured for this DbContext"

The problem:
The dotnet ef Migrations add MyNewMigration command fails with:
No database provider has been configured for this DbContext. A
provider can be configured by overriding the DbContext.OnConfiguring
method or by using AddDbContext on the application service provider.
If AddDbContext is used, then also ensure that your DbContext type
accepts a DbContextOptions object in its constructor and
passes it to the base constructor for DbContext.
There are many SO posts regarding this issue, and I have read most of them. The one that seems to have the exact same problem is here:
EF Core unable to run commands. Error: No database provider has been configured for this DbContext
However, the issue was never resolved. Here are some bullets that sums the investigation up and further down, details about the steps.
We had initially just hardcoded the connection string in the DbContext OnConfiguring method, and then the Migrations command worked well.
We then proceeded to use a static method, that read the appsettings.json file. This worked when running the app, but it did not work when running the Migrations add command, because the connectionString we fetched from the static class, always returned null.
We then moved to use dependency injection, like everyone everywhere suggests doing. in the Startup.cs, we use something like services.AddDbContext<MyDbContext> and in the MyDbContext we have a constructor like public MyDbContext(DbContextOptions<MyDbContext> options) : base(options). The constructor is called, the connectionstring is there, app can run, but Migrations Add fails with same error message as above.
I then tested removing the default empty constructor, just keeping the MyDbContext(DbContextOptions<MyDbContext> options) constructor. The error message from the command was then "Unable to create an object of type 'MyDbContext'."
Since I run the commands from my Data project (where entities, dbcontext etc exists), I tried adding a startup path to the command, like dotnet ef Migrations add MyMigrationStuff --startup-project C:\Git\MyProject\MyProject.Api, still without default/empty constructor. This time, the error message was simply Build failed. I then reinstated the empty construtor, ran same command again: then I get the same error as above.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// ... other stuff
string cString = configuration["ConnectionStrings:MyDb"]; // cString is correct and valid!
services.AddDbContext<MyDbContext>(options => options.UseMySql(cString, mySqlOptions => mySqlOptions
.ServerVersion(new ServerVersion(new Version(5, 0, 17), ServerType.MySql))));
// services.AddDbContext<MyDbContext>(); // without DI as in case 1 and 2 above
}
cString looks correct when running app:
MyDbContext:
public class MyDbContext : DbContext
{
// DbSet etc...
public MyDbContext() : base()
{
}
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
// If I break here, the options has two Extensions, looks ok, see below.
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
}
This is the options in the MyDbConstructor:
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"ConnectionStrings": {
"MyDb": "Server=localhost;Port=3308;Database=mydb;User=root;Password=root;"
}
}
I am running ASP.NET Core 3.1.101, EF Core 3.1.1, visual studio 2019.
you need to install MySQL provider using this cmd:
dotnet add package MySql.Data.EntityFrameworkCore
then add this code to your DBContext class constructor:
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseMySQL(Configuration.GetConnectionString("MyDb"));
}
finally, add this line into your startup.cs class instead of what you did put:
services.AddDbContext<MyDbContext>();

EF 7 alpha 3: Azure Table Storage

I'm trying to get an example of EF 7 with Azure Table Storage to work in VS 14 CTP3, but I am having no luck with the dependency injection stuff. I was able to get an example with SQL done fairly easily, but I am seeing an issue that doesn't make sense: The referenced package is there and being pulled in, and if I look at it, it contains the correct namespaces, methods, clases etc., but the compile doesn't like it.
Here is my project.json:
{
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-alpha3",
"EntityFramework.AzureTableStorage": "7.0.0-alpha3",
"Microsoft.AspNet.RequestContainer": "1.0.0-alpha3"
},
"frameworks" : {
"net451" : { },
"k10" : { }
}
}
using System;
using Microsoft.AspNet.Builder;
using Microsoft.Data.Entity; /* <- 'missing reference' unless I add EntityFramework to project.json */
using Microsoft.Data.Entity.AzureTableStorage; /* <- ALWAYS errors w/ 'missing reference' */
using Microsoft.Framework.DependencyInjection;
namespace WebApplication2
{
public class Startup
{
public void Configure(IBuilder app)
{
app.UseServices(services =>
{
services.AddEntityFramework() /* <-- this errors too */
.AddAzureTableStorage();
services.SetupOptions<DbContextOptions> //,- says it can't find this
(config => config.UseAzureTableStorage("UseDevelopmentStorage=true"));
});
}
}
}
The strange thing is, if I right click and 'go to definition' on any of the 'missing' classes or methods, it brings them up, and I can see that I'm using them as defined. Am I missing something terribly obvious? Or is this stuff just not fully cooked yet?
Your project.json has both frameworks mentioned so VS builds both of them. If your intention is to just build for net451, you should remove the following from your project.json -
"k10" : { }

Cannot access RavenDB Management Studio

Try:
I created a new project in VS2012
I installed via the NuGet package RavenDB Embedded -Pre
I installed Ninject.MVC3
Added a module for ninject RavenDB:
Public class RavenDBNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IDocumentStore>().ToMethod(context =>
{
NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);
var documentStore = new EmbeddableDocumentStore { Url="http://localhost:8080/", DataDirectory="~/App_Data", UseEmbeddedHttpServer = true };
return documentStore.Initialize();
}).InSingletonScope();
Bind<IDocumentSession>().ToMethod(context => context.Kernel.Get<IDocumentStore>().OpenSession()).InRequestScope();
}
}
In my class "NinjectWebCommon" ...
private static void RegisterServices(IKernel kernel)
{
kernel.Load(new RavenDBNinjectModule());
}
When running the application, the following url was generated ("http://localhost:1423")
Verify that the file "Raven.Studio.xap" was the root of my application
I tried accessing "http://localhost:8080" but the following screen is displayed:
What am I doing wrong?
As it turned out, the issue is that documentStore.Initialize never get called, because that no one did ask Ninject to resolve IDocumentStore.
You are setting the Url property, which means that you aren't running in embedded mode, but in server mode.
Remove the Url property, and everything will work for you.
I found the problem!
Since he had used IDocumentSession in no time, the ninject had not created the instance of IDocumentStore and thus not run the Initialize method