.csproj bug in Asp .NET Core MVC - asp.net-core

I have wanted to create new table in my database (SQLite), so I created new Model and used it in ApplicationDbContext class like this:
public DbSet<AppUser> AppUsers { get; set; }
public DbSet<Report> Reports { get; set; }
Then executed the command later:
dotnet ef migrations add Report
I can see migration file.
In Startup.cs file I have:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(connectionString));
In EFRepository.cs:
public IQueryable<AppUser> AppUsers
=> ctx.AppUsers;
public IQueryable<Report> Reports => ctx.Reports;
After this somehow suddenly I received many errors connected with this file ( .csproj):
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>Mushrooms</AssemblyName>
<RootNamespace>Mushrooms</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
I checked dotnet --info command, but I can't see incompatibility.
Three of the errors:
Invalid markup declaration '<'
Invalid markup declaration '
Invalid token "" at root level of document.
and many more in every line of this file.
This errors have occured when system was starting up.
I am using Asp .NET Core MVC 3.0.0.
Before this everything was working fine, any suggestions?

I try to create blank .net core 3.0 project and this csproj file is working fine
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
</ItemGroup>
</Project>
Try to check your project extension file should be .csproj

Related

Why I am getting there was an error running the selected code generator method not found?

I am new to razor pages.
I am fowlling the tutorial
https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-6.0&tabs=visual-studio
When I try to add Right-click on the Pages/Movies folder > Add > New Scaffolded Item.
I am getting the following error :
There was an error running the selected code generator Method not found void ?
Here are my packages refrences :
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="cloudscribe.Core.Storage.EFCore.MSSQL" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-preview.7.22376.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="6.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Pages\Movies\" />
</ItemGroup>
</Project>
I implemented the soultion in the there was an error running the selected code generator in .net6
Now I am getting another error:
There was an error runinig the selected code generator scafolding faild in type microsoft.CodeAnalysis.CSharp ..

How to exclude folders from Visual Studio 2022 WebDeploy?

I don't manage to exclude certain folders from Visual Studio 2022 WebDeploy (publish to IIS).
There is a bunch of folders that I do not want to be published to my server (e.g. jquery, bootstrap-icons etc.).
I followed this post and ended up with a new xml file. The file is called [MyProjectName].wpp.targets.xml and it is located in the same folder as my [MyProjectName].csproj.
The file looks like this:
<Project ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ExcludeFromPackageFolders
Include="wwwroot\lib\jquery;
wwwroot\lib\jquery-validate;
wwwroot\lib\jquery-validation-unobtrusive;
wwwroot\lib\jquery-ajax-unobtrusive;
wwwroot\lib\popper.js;
wwwroot\lib\bootstrap-icons">
</ExcludeFromPackageFolders>
</ItemGroup>
On my next Publish, however, all those folders (and included files) are still being copied to my server.
What am I doing wrong?
I am using Visual Studio Community 2022, V.17.1.5, with a .NET 6 application.
Edit your {project_name}.csproj file and add the following lines...
<ItemGroup>
<Folder Include="wwwroot\Files\" Exclude="Files\**\*" />
</ItemGroup>
With "wwwroot\Files" being the location of the folder with the files you want to exclude.
This can be anywhere inside your top level declaration.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.32" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.32" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
<PackageReference Include="System.Linq" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PCS.BusinessServices\PCS.BusinessServices.csproj" />
<ProjectReference Include="..\PCS.Common\PCS.Common.csproj" />
<ProjectReference Include="..\PCS.Data.Services\PCS.Data.Services.csproj" />
<ProjectReference Include="..\PCS.Data\PCS.Data.csproj" />
<ProjectReference Include="..\PCS.Encryption\PCS.Encryption.csproj" />
<ProjectReference Include="..\PCS.PdfEngine\PCS.PdfEngine.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\Files\" Exclude="Files\**\*" />
</ItemGroup>
</Project>

IWebHostEnvironment .Net 5 not found

I am using .Net 5 class library project and here are my refrences. I am using
using Microsoft.AspNetCore.Hosting; But I cannot find IWebHostEnvironment. What wrong am I doing ?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.12.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Microsoft.Graph" Version="3.19.0" />
<PackageReference Include="Microsoft.Graph.Core" Version="1.22.0" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.8" />
<PackageReference Include="Microsoft.ReportViewer.WebForms" Version="10.0.40219.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
<PackageReference Include="System.ServiceModel.Http" Version="4.8.1" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.8.1" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ServiceModel" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>
On .Net5.0, IWebHostEnvironment has been replaced by IHostEnvironment
Make sure you have the package Microsoft.Extensions.Hosting installed and the following import on the top of the file.
using Microsoft.Extensions.Hosting;
More information can be seen at aspnetcore github discussion
IWebHostEnvironment is included in the Microsoft.AspNetCore.Hosting package, you simply need to add it as a reference to your project by right clicking on your project file and selecting 'Manage Nuget Packages' then searching for Microsoft.AspNetCore.Hosting and add it.
If you've already added it and it still isn't working, try cleaning your project.

'PublishAsync' missing in RabbitMq package

I have the following code:
public async Task<IActionResult> Post([FromBody]CreateActivity command)
{
command.Id = Guid.NewGuid();
command.CreatedAt = DateTime.UtcNow;
await _busClient.PublishAsync(command);
return Accepted($"activities/{command.Id}");
}
It seems like 'PublishAsync' method is missing in RabbitMq. This is my .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="RawRabbit" Version="2.0.0-beta8" />
<PackageReference Include="RawRabbit.DependencyInjection.ServiceCollection" Version="2.0.0-beta8" />
<PackageReference Include="RawRabbit.Operations.Subscribe" Version="2.0.0-beta8" />
</ItemGroup>
</Project>
It is the same with all other stable versions.
Looking at the source code it seems that the PublishAsync method is defined as an extension method in the RawRabbit.Operations.Publish NuGet pacakge:
Enrich the BusClient with PublishAsync, used to perform a BasicPublish

ASP Core migration 1.1 to 2.0 : Controller/Views scaffolding multiple exceptions

I migrate my asp core 1.1 project to 2.0 with success, I can build, and launch debug with no problem.
But when using the scaffolding to generates Controllers and Views (VS Community 2017)
Add > Add Scaffolded item > MVC Controller with views, using Entity Framework
I've got several exceptions when launching the process on any POCO, as example I picked the first thrown exception
=> I changed the default templates to match my pattern (ie DI of generic Service Layer within each controller) and on the project before migration (on asp core 1.1) everything worked fine.
There was an error running the template C:\Users\Nicolas\Documents\Visual Studio
2017\Projects\WebPortal\WebPortal\Templates\ControllerGenerator\MvcControllerWithContext.cshtml: Template Processing
Failed:Template(45,31): error CS0246: The type or namespace name 'List<>' could not be found (are you missing a using
directive or an assembly reference?) Template(65,33): error CS0246: The type or namespace name 'Dictionary<,>' could
not be found (are you missing a using directive or an assembly reference?) Template(118,28): error CS0246: The type or
namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?) at
Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0() at
Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at
Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at
Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
And the concerned lines of my Controller template sued for the scaffold:
var modelProperties = new List(); (line 45)
var relatedProperties = new Dictionary<string, dynamic>(); (line 65)
var dependencies = new List(); (line 118)
Obviously I import the corresponding namespace for List and Dictionary.
If I remove these lines I've got other exceptions which were not thrown when the project was on .net core 1.1 version.
Here is the .csproj for 2.0 project (scaffolding fails)
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
</PropertyGroup>
<PropertyGroup>
<UserSecretsId>aspnet-WebPortal-ed2c59f1-3859-46e1-af11-xxxxx</UserSecretsId>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
And the .csproj for 1.1 project (scaffolding works with same template)
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>
<PropertyGroup>
<UserSecretsId>aspnet-WebPortal-ed2c59f1-3859-46e1-af11-xxxxx</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>
</Project>
Anyone could help on that point ? I can supply the templates off course.
Regards
I finally managed to make my template works on .net core 2.0, I checked the differences between my templates and the templates from the repo Templates Github repo, and here are the mandatory modifications :
Templates\ControllerGenerator\MvcControllerWithContext.cshtml
Add :
#using System.Collections.Generic;
#using System.Linq;
in addition of :
using System.Collections.Generic;
using System.Linq;
So the 'header' looks like :
#inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
#using System.Collections.Generic;
#using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
Templates\ViewGenerator\List.cshtml
Add :
#using System.Collections.Generic;
#using System.Linq;
so the 'header' looks like :
#inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
#using Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore
#using System.Collections.Generic
#using System.Linq
##model #getenumerabletypeexpression(Model.ViewDataTypeName)
Then make the following replacement :
IEnumerable<PropertyMetadata> properties = Model.ModelMetadata.Properties;
replaced by
var properties = Model.ModelMetadata.Properties;
I didn't have to change anything else to my other view templates, and I don't use other controller templates