.Net Core
Matters related to .Net Core
-
Dotnet EF tools updating
I don’t know why this is always hard to find….
## To Install dotnet tool install --global dotnet-ef ## To Update dotnet tool update --global dotnet-ef
~ScottGeek
-
Dot Net Hell by any other name?
Just when I think Dot Net Hell days are coming behind us… I run across the reality of the fact that those that created this in the first place are… well… still doing it?
Take this little gem:
public static async IAsyncEnumerable<string> GetContent(string[] urls) { }
Yes, we now have IAsyncEnumerable… or do we. In my little experiment, I have a .Net 4.8 console app with C#8.0 language stuff turned on. So, I wanted to mess about with the Async Enumerable…. But in my console app that IAsyncEnumerble goes all red line of crap on me.
So what’s the deal? Well it’s simple once you do the digging. .Net 4.8 does not implement .Net Standard 2.1… Yeah, you guessed it… where the Async Enumerable lives!
So yeah, I could use .Net Core 3.0 which works with C# 8.0 and .Net Standard 2.1. Or here’s a thought why not just do a .Net Standard 2.1 console app? Well, the latest version of VS 2019 does not have a .Net Standard 2.1 template listed… Ummm, I’m sensing something missing there.
Welcome to the “New” Hell!
~ScottGeek….
-
Dotnet Core Templates – The Revisit
So a while back I penned an article (Dotnet new Magic), but now with the latest VS 2019 and Dotnet Core 3.x release… it’s time to revisit.
In case we forgot – the command is:
dotnet new -i “–The Template Reference Name Here–”
The list of templates can be found in the old place but also here – Dotnet New Templates
What ones do I add:
dotnet new –install “Microsoft.AspNetCore.SpaTemplates”
dotnet new –install “Microsoft.Azure.WebJobs.ProjectTemplates”
dotnet new –install “Microsoft.Azure.WebJobs.ItemTemplates”
dotnet new –install “NUnit3.DotNetNew.Template”
dotnet new –install “Microsoft.AspNetCore.Blazor.Templates” **Some of these already exist, but this one adds the webassembly type.
dotnet new –install “Microsoft.Azure.IoT.Edge.Module”
dotnet new –install “Microsoft.Azure.IoT.Edge.Function”
dotnet new –install “RaspberryPi.Template” -
.Net Core ASP Tag Helpers
I has to be one of the most useful things added… You got Tag Helps…
Consider the simplest example…
<img src="/images/somePng.png" />
Not hard and very common. But consider the task of versioning your images for a web page… Enter Tag Helpers
<img src="/images/somePng.png" asp-append-version = "true"/>
This gives us a Hash version appended, so if an image changes? Indeed yes the hash value changes and this will trigger a cached page to update the image.
Wow, Tag Helpers… very handy.
ScottGeek
-
Port Desk Top UI App
Not really ready for prime time , but here’s an article stepping through the how-to and the workaround for the fact that Core has yet to add the visual designer for, ya you guessed the actual Windows Form- that seems to be a rather glaring miss if we are talking about porting and doing traditional windows UI forms development going forward.
In this post, I will describe how to port a desktop application from .NET Framework to .NET Core. I picked a WinForms application as an example. Steps for WPF application are similar and I?ll describe what needs to be done different for WPF as we go. -
Play with dotnet tool and a BOT
I was poking about with cleaning up my DotNet Core sdks and in my travels a found this thing call dotnet tools… Let the experiment begin… To credit the source of an article on Microsoft go here: dotnet tool
Do a console project with dotnet:
dotnet new console -o botsay
Then, of course, cd into the botsay directory… and edit the Program.cs (yeap you can do this in VS 2017)
Here’s the main method:
using System.Reflection;
static void Main(string[] args)
{
if (args.Length == 0)
{
var versionString = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion
.ToString();Console.WriteLine($"botsay v{versionString}");
Console.WriteLine("-------------");
Console.WriteLine("\nUsage:");
Console.WriteLine(" botsay <message>");
return;
}ShowBot(string.Join(' ', args));
}The ShowBot method is this:
static void ShowBot(string message) { string bot = $"\n {message}"; bot += @" __________________ \ \ .... ....' .... .......... .............'..'.. ................'..'..... .......'..........'..'..'.... ........'..........'..'..'..... .'....'..'..........'..'.......'. .'..................'... ...... . ......'......... ..... . _ __ ...... .. # ## ...... .... . ....... ...... ....... ............ ................ ...................... ........................'................ ......................'..'...... ....... .........................'..'..... ....... ........ ..'.............'..'.... .......... ..'..'... ...............'....... .......... ...'...... ...... .......... ...... ....... ........... ....... ........ ...... ....... '...'.'. '.'.'.' .... ....... .....'.. ..'..... .. .......... ..'........ ............ .............. ............. '.............. ...........'.. .'.'............ ............... .'.'............. .............'.. ..'..'........... ............... .'.............. ......... .............. ..... "; Console.WriteLine(bot); }
It’s a simple core console app that takes a text phrase as input and shows the bot saying it.
Now the fun part… Of course, you should make sure that the code compiles.
Go back to your command window and do the folllowing:dotnet run dotnet run -- "Hello from the bot" dotnet run -- hello from the bot
It’s a Bot!
Now… let’s make a dotnet tool…
Edit your botsay.csproj file. You can do this in VS 2017 or your favorite notepad…
You need to add these lines:
<PackAsTool>true</PackAsTool>
<ToolCommandName>botsay</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>In the end the csproj file show look like:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> <PackAsTool>true</PackAsTool> <ToolCommandName>botsay</ToolCommandName> <PackageOutputPath>./nupkg</PackageOutputPath> </PropertyGroup> </Project>
Now the tool making part:
dotnet pack dotnet tool install --global --add-source ./nupkg botsay You can invoke the tool using the following command: botsay Tool 'botsay' (version '1.0.0') was successfully installed.
Once you get the message back from the “dotnet tool”… command you now have a dotnet tool…
** You may have to close the command window and open a new one… depending on your OS, etc.You can type botsay –You’re Message Here…
Now, as you experiment don’t forget to clean up
dotnet tool uninstall -g botsay
You should read more about dotnet tool… start here: dotnet tool on Mircosoft docs
-
.Net Core 3 the next version
I still have not gone down the full .Net Core 3 path- but soon yes… Meanwhile MSDN mag has good preview on the topic:
-
Time to talk Net Core 3?
Is it time to talk… some good looking things coming for .Net Core 3 – the deeper reach into the UI with added compatibility for Windows Forms and WPF. Have a read….
Are your Windows Forms and WPF applications ready for .NET Core 3.0?
A first-hand look from the .NET engineering teams -
.net Core 2.1 and SQL via EF Core 2.1 -The revisit
It’s time to revisit this topic but for the latest up dates for .Net Core (v 2.1.1) and EF Core (v 2.1)
Let’s start with the common project using, in my case, VS 2017 15.7.4. Start a .Net Project based on v 2.1.x (I currently have installed .Net Core SDK v 2.1.301
(Gee think we have enough versions of things 😯 )I’m doing a .Net Core Class so I can get my DBContext and Table models in place….
We start with some Packages:Microsoft.EntityFrameworkCore.SqlServer (2.1.1) Microsoft.EntityFrameworkCore.SqlServer.Design (1.1.5) Microsoft.EntityFrameworkCore.Tools (2.1.1)
Next, time for a database and let’s Scaffold a DB Context and our models… On the .Net Core project start up the Package Manager Console.
dotnet ef dbcontext scaffold "Server={ServerName};DataBase={DataBaseName};Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models --table {table1Name} --table {table1Name} --table {table1Name} .....
For Connection Strings to SQL with a user/pass, an example would be "Server={ServerName;DataBase={DataBaseName}g;Integrated Security=False; User ID={sql user};Password={sql user Pass}; Connect Timeout=30;Encrypt=False;TrustServerCertificate=True; ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
The Walk-Through…
Line 1 – dotnet ef dbcontext scaffold This is the start of the command. As we see dotnet is the actual start of the SDK utility. It is worth looking this up. “ef dbcontext scaffold” is how we start the EF scaffold. Line 2 – The Connection String **1
“Server={ServerName};DataBase={DataBaseName};Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False”Line 3 – This is the provider used for MS SQL server. Yes it would be different if another database was being used. Line 4 – The –output param is giving us a way to make sure we put all models/dbcontext in a folder in the project. Line 5 – The –table allows one to generate models (and DbContext) for only certain tables in the DB. **1 This is the standard SQL connection string. This will take on various forms depending on what you are connecting to. And yes, one can use the localdb.
-
Dotnet Core Templates, or DOTNET new -i “the magic”
Time to touch on this topic again… because every time a new DOTNET SDK comes out I seem to lose the new templates. I understand the why, and ya I wish the .Net Core group would fix it.
The “why”- each time one updates to a new Core SDK, the template scaffolds get placed into a new folder (usually under your users directory in a annoying place like ….\.templateengine\dotnetcli\”version of sdk”\). So each time you move to a new DOTNET SDK, this becomes a problem.
The second part of of DOTNET new -i that is an issue, is if you have different nuget repositories set in VS. It’s a great feature in VS 2017 where one can add additional package management sources… problem is with the DOTNET command line when you want to bring in new templates.
So, why am I going on about this? Well as I move from SDK version to version… I would like the damn templates I’ve added to either get updated (the standard ones do), or at least make sure they are available to the new SDK…. How hard can it be….
So here’s the list of what I keep installing from (dotnet Templates on github)
Here’s my list:
dotnet new -i “Microsoft.AspNetCore.SpaTemplates::*” – this one always
dotnet new -i “Boxed.Templates::*” – I’m still undesided about boxed.
dotnet new -i Microsoft.AspNetCore.Blazor.Templates::* – nice little framework that has become a 1st class citizen in VS. Make sure you also check out the VS extension.
dotnet new -i “RaspberryPi.Template::*” – who does not like Iot stuff. Also available in VS as a 1st class citizen.
Update: So as we now have SDK version… 2.1.400 I notice that some of the SPA templates remain off the list…
The SPA templates after SDK update:
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPAAnd the templates after the dotnet new -i of the SPA’s:
ASP.NET Core with Aurelia aurelia [C#] Web/MVC/SPA
ASP.NET Core with Knockout.js knockout [C#] Web/MVC/SPA
ASP.NET Core with Vue.js vue [C#] Web/MVC/SPA
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPAThe ones in bold seem to gone still…. oh well.