FileExtension – GUI–part 4

Now I must do something for the people that just want to use the software. The most usual , for me, it is a .NET Core WebAPI site ( with swagger ) and an Angular frontend.

You can find the sources at https://github.com/ignatandrei/FileExtension/tree/master/src . Some thoughts:

For .NET Core ( see https://github.com/ignatandrei/FileExtension/tree/master/src/RecognizeFileExtWebAPI ) I use the following NuGET packages ( other than usual .NET Core)

Microsoft.AspNetCore.Mvc.Versioning

Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer

Swashbuckle.AspNetCore

Hellang.Middleware.ProblemDetails

AMSWebAPI

AMS_Base

RSCG_AMS

The final result can be seen at https://fileextension.azurewebsites.net/swagger

For Angular( https://github.com/ignatandrei/FileExtension/tree/master/src/FileExtensionAng) I use the following packages( other than usual Angular)

@angular/material

angular-shepherd

The final result can be seen at https://fileextension.azurewebsites.net/

FileExtension–plugins–part 3

What I have more in mind is that the people can add easy their code for extensions.

That means:

  1. Have an easy class to add the magic number in the header for an extension
  2. Have other people add their own implementations, not just the header
  3. Have an easy way to add the project with their code

 

First item it is easy – just create a class RecognizeFirstBytes and pass into constructor the first bytes

As an example , see the sln implementation


    public class RecognizeSln : RecognizeFirstBytes
    {
        public RecognizeSln() : base("EFBBBF0D0A4D6963726F736F66742056697375616C2053747564696F20536F6C7574696F6E2046696C65")
        {
            Extension = new string[1] { "sln" };
        }
    }

The second item can be implemented with an interface IRecognize


    public interface IRecognize: IEquatable<IRecognize>
    {
        Result InfoNeeded(byte[] b=null);
        string[] Extension { get; set; }
    }

The third item can be made by  making a third project  that  either references all other projects, either loading others as plugins . Because of the experience, I choose the most interesting one – plugins. I need first https://github.com/natemcmaster/DotNetCorePlugins . Second I need the plugins to be copied – I use a Target

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="xcopy &quot;$(ProjectDir)../plugins/*.dll&quot; &quot;$(ProjectDir)plugins&quot; /E /F /I /Y /R" />
  </Target>

And last I need this plugin loader , when packed as nuget  at https://www.nuget.org/packages/FileExtension/ , to have also the plugins – for this I use the csproj

<ItemGroup>
  <Content Include="plugins\**\*.dll">
    <PackagePath>build\net5.0\plugins\</PackagePath>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

FileExtension– export data from CSV–part 2

First, is how to export data from table (https://en.wikipedia.org/wiki/List_of_file_signatures) into classes . First, the table must be transformed into a more programmatic recognizable way – like a CSV  – see https://github.com/ignatandrei/FileExtension/blob/master/src/RSCG_GCK/offset0.txt

Then a solution is to have into the dll ( as embedded resource or as a file) . The second solution, more complicated , is to use Roslyn Source Code Generators to generate the classes for each element of the CSV .

I’d liked more the second solution – the sources are at https://github.com/ignatandrei/FileExtension/tree/master/src/RSCG_GCK 

What it generates is like this

namespace RecognizeCustomSigs_GCK {
    class RecognizeFromGCKLine0_JP2: RecognizeFromLineCustomsigs{
        public RecognizeFromGCKLine0_JP2(): base("JPEG2000 image files,00 00 00 0C 6A 50 20 20,JP2")                
        {
        }
    }
}            

FileExtension–idea- part 1

Every now and then we accept file uploads ( to a site  ) – or we have files in outlook that we want to open – but we do not know if the extension matches the content of the file.

There are numerous way to do it , however , I have been attracted by Magic Numbers – a header into the file that says what is the kind of the file.

For example, a .sln file has always those characters inside:

Microsoft Visual Studio Solution File, Format Version

You can then recognize if it is a real sln if it contains those characters.

Reading more on the internet, I have found

https://www.garykessler.net/library/file_sigs.html

https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files

https://en.wikipedia.org/wiki/List_of_file_signatures

Now, there are multiple programs and sites to do this magic number verifier – however, in order to practice programming, is always a good idea to start somewhere – so let’s see where it goes.

You can find the sources at https://github.com/ignatandrei/fileExtension/

The application is live at https://fileextension.azurewebsites.net/static/v1

You can find a NuGet package at https://www.nuget.org/packages/FileExtension/

Evolving Roslyn Source Code Generators

I am a big fan of Roslyn Source Code Generators – you can read about at https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md and https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/. I have tested , until now, 27 examples and discovered other 53 RSCG – you can read about at https://github.com/ignatandrei/RSCG_Examples and download the book from  https://ignatandrei.github.io/RSCG_Examples/

Also, Microsoft is dogfooding this by using at Blazor ( https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-2/#razor-compiler-updated-to-use-source-generators ), JSON.NET https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/ . This will alleviate some of the pain points from https://turnerj.com/blog/the-pain-points-of-csharp-source-generators .

Now Microsoft is evolving the specification . I have read about Incremental Generators – https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md . This sounds promising – and the fact that is called “Incremental” , that can generate more than source code and can be cached sounds great. Let’s see !

RSCG Example – AOPMarker for CI Builds – part 30

 

 

name AOPMarkerCI
nuget

https://www.nuget.org/packages/AOPMethodsCommon/
https://www.nuget.org/packages/AOPMethodsGenerator/

link http://msprogrammer.serviciipeweb.ro/category/roslyn/
author Andrei Ignat

This will tracing methods marked with AOPMarkerMethod in CI builds. Does not affect the code run by the programmer.
 

The code that you start with is


    using AOPMethodsCommon;

    using System;

    using System.Threading.Tasks;

    

    namespace AOPMarkerCI

    {

        [AutoMethods(template = TemplateMethod.CustomTemplateFile, MethodPrefix = "auto", CustomTemplateFileName = "../AutoMethod.txt")]

        partial class UnderTest

        {

            [AOPMarkerMethod]

            public async Task<int> Method1()

            {

                await Task.Delay(1000);

                var ret = Method2(DateTime.Now);

                return ret % 2 == 0 ? 1 : 0;

            }

            [AOPMarkerMethod]

            private int Method2(DateTime now)

            {

                return now.Second;

            }

    

        }

    }


The code that you will use is



    Console.WriteLine("Run the autoci file");

    var underTest = new UnderTest();

    int i = await underTest.Method1();

    Console.WriteLine($"result:{i}");

 

The code that is generated is


    //------------------------------------------------------------------------------

    // <auto-generated>

    //     This code was generated by a tool.

    //

    //     Changes to this file may cause incorrect behavior and will be lost if

    //     the code is regenerated.

    // </auto-generated>

    //------------------------------------------------------------------------------

    using System;

    using System.Collections.Generic;

    using System.CodeDom.Compiler;

    using System.Runtime.CompilerServices;

    using System.Diagnostics;

    namespace AOPMarkerCI {

    

        [GeneratedCode("AOPMethods", "2021.6.11.907")]

        [CompilerGenerated]

        public partial class UnderTest{

            //autoMethod1

                

            public  async  System.Threading.Tasks.Task<int> Method1 (){

    

         Console.WriteLine("start method autoMethod1 at " +utcTime);

    

                try{

                     return   await  autoMethod1();

                }

                catch(Exception ex){

                    Console.WriteLine($"--autoMethod1 exception {ex.Message}");

                    throw;

                }

                finally{

                        utcTime =System.DateTime.UtcNow;

                    Console.WriteLine("end method autoMethod1");                

                }

    

    

            }//end Method1

            

                    //autoMethod2

                

            public  int Method2 (System.DateTime now){

                   var utcTime =System.DateTime.UtcNow;

                 

                   Console.WriteLine("start method autoMethod2 at " +utcTime);

                    string valnow ;

                    try{

                        valnow = System.Text.Json.JsonSerializer.Serialize(now);

                    }

                    catch(Exception ex){

                        valnow = "Error serializing parameter now : "+ ex.Message;

                    }

                    Console.WriteLine("Argument_now :" + valnow);

                    

                 

                

    

                try{

                     return   autoMethod2(now);

                }

                catch(Exception ex){

                    Console.WriteLine($"--autoMethod2 exception {ex.Message}");

                    throw;

                }

                finally{

                        utcTime =System.DateTime.UtcNow;

                    Console.WriteLine("end method autoMethod2");                

                }

    

    

            }//end Method2

            

            

        }

     }

    

    

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/AOPMarkerCI

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators

[ADCES] 12 oct, FluentAssertions.Web & Bigdata2Snowflake

Prezentare 1 : Test like a Pro .NET APIs with FluentAssertions.Web
Prezentator: Adrian Iftode, https://csharpin.blogspot.com/
Descriere: With more than 100 million downloads, it is less likely you’ve never heard of Fluent Assertions. But did you know you could write your own extensions? With Fluent Assertions Extensions, you could create an assertions language that fits your business domain, by reusing the same idioms as your production code and by wrapping the business rules into simple and reusable assertions. In this presentation, we will check FluentAssertions.Web, an extension that asserts the .NET HTTP API responses, why it was built, how it can be used, and then we will deep dive into its internals in order to learn how you can build your own extension

Prezentare 2 : Moving the bigdata (lake) to cloud: Our transition to Snowflake
Prezentatori :Ionuț Bobaru & Ion Anghel (Ubisoft)
Descriere: One year ago we reached an important milestone for our projects and we realized it was time to make a decision: continue with Teradata or switch to a newer technology? Join our meetup and discover how we migrated from Teradata to Snowflake. We will tackle topics such as: architecture, stored procedures, lambda functions, step functions, Snowflake external functions, exporting and importing data to AWS S3 and others. See you there!

Va astept la https://www.meetup.com/Bucharest-A-D-C-E-S-Meetup/events/280404415/

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.