HaintonDotNet - September 2022

HaintonDotNet - September 2022

This article was originally published 21st June 2022 on Linkedin

Modern .NET and WPF - Peter Bull

Modern .NET and WPF was my talk about using the latest .NET with WPF or Windows Presentation Foundation. I run tutorialr.com which has tutorials, talks and workshops and can find me on Twitter @RoguePlanetoid and were also raising awareness The Cyber Helpline which provides free, expert help for victims of cybercrime & online harm and can find them on thecyberhelpline.com.

.NET

.NET was first released back in 2002 and 2022 is the twentieth anniversary of .NET, which is a free developer platform, which has been open source since 2016. .NET 6 is the latest version of .NET but .NET 7 will be coming in November 2022 brining more improvements, there is a major .NET version every year with even-numbered releases supported for three years and odd-numbered ones for one year. You can build apps for Web, Android, iOS, macOS, Linux and Windows and can find the latest version at dot.net.

Web Applications

ASP.NET is the framework for building web apps in C# using .NET, it is fast and scalable supporting over 7 million requests per second, compared to Java Servlet's two million requests per second and Node.js half a million. ASP.NET web apps can be created with MVC or Model View Controller with views using Razor and HTML or can use Razor Pages for modern web apps. You can also create REST APIs and take advantage of minimal APIs for a smaller implementation. You can find out more at asp.net.

Blazor allows you to create client web apps using C# and .NET and create web interfaces without JavaScript, but can use JavaScript APIs or libraries when needed. Blazor can run client side using WebAssembly or server side by using SignalR to update the UI. Blazor Web components can be brought to applications with Blazor Hybrid in .NET MAUI or to modernise Windows Forms and Windows Presentation Foundation apps. You can find out more at blazor.net.

Cross-Platform Applications

Avalonia allows you to create high-fidelity experiences with C# and XAML with this third-party platform by leveraging existing WPF skills and create them using Visual Studio or in JetBrains Rider. You can create apps for macOS, Linux, Raspberry Pi, Web Assembly, iOS, Android or Desktop on Windows 10 or 11 with some experimental support for Windows 7. You can find out more at avaloniaui.net.

Uno Platform allows you to create pixel-perfect multi-platform apps with C# and XAML for WinUI with this third-party platform and leverage existing skills such as those from the Universal Windows Platform to create more modern applications with WinUI and bring them cross platform. You can use Visual Studio, Visual Studio Code and JetBrains Rider and target major operating systems such as macOS, iOS, Android, Linux or Windows 10 and 11 with WinUI or WPF for Windows 7 or modern browsers. You can find out more about Uno Platform at platform.uno.

.NET MAUI is the newest addition to the .NET ecosystem and is the successor to Xamarin to create native applications of hybrid experiences using Blazor and target Android, iOS, macOS and Windows 10 and 11 and can find out more about .NET MAUI at dotnet.microsoft.com/apps/maui.

Windows Applications

Windows App SDK is the next evolution of the Windows application development platform, it is the successor to Universal Windows Platform, you can build native apps for Windows 11 and down-level to Windows 10 1809, it has regular updates not tied to operating system releases and leverages the latest platform and design features with WinUI and can find out more at learn.microsoft.com/windows/apps/windows-app-sdk.

Windows Forms started back in 2002 but was made open-source in 2018 and you can build fully native applications for Windows or create hybrid experiences with Blazor, you can create applications for the latest version of Windows or for older versions of Windows. Find out more about Windows Forms at learn.microsoft.com/dotnet/desktop/winforms.

Windows Presentation Foundation started in 2006 and modern .NET supports WPF where you can build native applications or create hybrid experiences with Blazor. WPF supports the breadth and depth of Windows platform features but with separation of code between XAML and C# it would be possible to migrate to something like Uno Platform in the future as features / requirements mature. You can find out more about WPF at learn.microsoft.com/dotnet/desktop/wpf.

Demo

A simple desktop notes application written in WPF was then shown off which supports data binding of XAML, as well as more modern features such as modern UI styles thanks to the work of other developers and many more modern .NET features. This included showing off Blazor Hybrid being used, but also more unexpected combinations such as the application service not only its own ASP.NET Minimal API but also a full Razor Pages experience, which also supported rendering of Windows Presentation Foundation components and a stand-alone Razor Pages example was also shown with the same functionality. You can download demo and slide from github.com/RoguePlanetoid/moderndotnetandwpf.

Manage your energy using .NET and Google Cloud - Mark Thompson

Mark Thompson, @mthompson on Twitter, is the director of software development at Ideanomics, who create wireless chargers and electric vehicles, who talked about managing your energy using .NET and Google Cloud.

Energy and Installation

Octopus Energy offers agile tariffs to customers, they also have an Export Tariff - also known as a Feed In Tariff, that allows customers to sell back their excess energy back to the grid. Due to usage and demand requirements the best time to sell energy back to the grid is at around 5:30 pm. So if you have batteries for a solar panel installation you can store excess energy then make money by sending this back to the grid at the right time, you can determine this by getting information and prices for the next 24 hours by querying the Octopus Energy API which will show the prices for the next 23 hours and use this information to know when to export energy back to the grid, you can find out more about Octopus Energy at octopus.energy.

Energy is measured in kilowatts and the amount of energy generated is measured in Kilowatt Hours, this is how long the energy can be supplied, so a 10Kwh battery for example can deliver 1Kw of power for 10 hours. The average installation can have solar panels, battery storage and an inverter - which by law has to be disabled when there is a power cut as cannot have 240 volts running through if someone might be working to resolve the power cut. You will need a letter from your DNO (Distribution Network Operator) before you can sell energy back to the grid as well using an MCS certified installer and it is recommended you get plenty of quotes before proceeding with an installation. If you are generating anything above 3.6Kw you will need permission from your DNO before selling back to the grid. Another factor is if one part of a solar array isn't generating enough energy, such as is in shadow or covered in some way this will affect the output of the entire array but it is possible to add micro-inverters so that a single panel having issues will not degrade the generation of the entire array, but this will add more cost to the installation.

Development

Octopus Energy have made using their API with C# quite easy and there is plenty of documentation, in this case Fiddler was used to get the requests and then replicate these using HttpWebRequest with C# along with using json2csharp.com to convert the JSON from the responses to C# classes for use with serialisation and then perform any requests and populate those objects from the API. Inverter is a Lux Inverter which although doesn't have an API or documentation, it does have a web user interface which has setting that allows the battery to be discharged at a certain time, by using this when running Fiddler can intercept the requests and then these can be replicated using C#, this can also be done with the authentication process to allow this to be replicated also. You can find out more about Lux Inverters at luxpowertek.com.

Google Cloud Platform

It is quite straightforward to modify an ASP.NET Core Web Application to run on Cloud Run for Google Cloud, all that is needed is to be able to accept the port number as an environment variable. The application will show energy pricing for the next 24 hours and will allow the user to select a time to discharge the battery when energy will be at the best price. Cloud Run is quite cost-effective as a development or even a light-use application can be free to run.

Will need to install the Google Cloud Platform SDL and the CLI, then modify the app to run on Cloud Run for Google Cloud, Build and Deploy it to Cloud Run. From the CLI just need to execute the gcloud run deploy command which builds and sends the application to Cloud Run on the Google Cloud Platform. When you do this it will ask you to select a region to run the application in, as well as asking to allow unauthenticated invocations to access the web application, you can restrict it to work from only certain Google accounts but can change this later, You can also provide additional configuration simply and easily by passing in additional Environment variables to configure the web application, you can find out more about the Google Cloud Platform at cloud.google.com.

Demo

The web application can be used from Google Cloud Platform to see the energy prices for the next twenty four hours and then to select the one with the best price which will then instruct the inverter to discharge the battery into the grid at the appropriate time. You can download the code for the demo at github.com/mthompson2/EnergyDemo.