.Net Core

Matters related to .Net Core

  • .Net,  .Net Core,  ASP.NET Core,  Microsoft,  Visual Studio 2019

    Dot Net 5 is Here… Check it out.

    Wow… some most excellent videos on what is what in .Net 5! Check it out on

    You can also find these on You Tube… search on .Net 5. The Conf 2020 Days 1-3 will be there…

    ~ScottGeek.

  • .Net,  .Net Core,  Microsoft,  Visual Studio 2019

    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….

  • .Net Core,  Microsoft

    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,  Microsoft

    .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

     

  • .Net Core,  Article,  Microsoft

    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.

     

  • .Net Core,  Microsoft

    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&lt;AssemblyInformationalVersionAttribute&gt;()
    .InformationalVersion
    .ToString();

    Console.WriteLine($"botsay v{versionString}");
    Console.WriteLine("-------------");
    Console.WriteLine("\nUsage:");
    Console.WriteLine(" botsay &lt;message&gt;");
    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:

      &lt;PackAsTool&gt;true&lt;/PackAsTool&gt;
    &lt;ToolCommandName&gt;botsay&lt;/ToolCommandName&gt;
    &lt;PackageOutputPath&gt;./nupkg&lt;/PackageOutputPath&gt;
     

    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,  Microsoft

    .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.