European ASP.NET 4.5 Hosting BLOG

BLOG about ASP.NET 4, ASP.NET 4.5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

European ASP.NET Core Hosting - HostForLIFE :: gRPC Introduction And Implementation Using .NET Core 6

clock September 26, 2022 09:24 by author Peter

We are going to discuss the gRPC and its implementation using .NET Core 6

Agenda
    Introduction of gRPC
    Different scenarios in which we use gRPC
    Pros and Cons of gRPC
    Implementation of gRPC

Prerequisites

    Visual Studio 2022
    Basic Knowledge of C#

Introduction

    gRPC stands for Google Remote Procedure Calls
    gRPC is a modern open-source high-performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking, and authentication. It is also applicable in the last mile of distributed computing to connect devices, mobile applications, and browsers to backend services. – gRPC Page

 

    gRPC is the framework that is used to implement APIs using HTTP/2
    Basically, gRPC uses the protobuf for serialization and HTTP2 protocol which provides lots more advantages than HTTP
    gRPC clients and servers intercommunicate using a variety of environments and machines, It Also supports many languages like Java, C#, Go, Ruby and Python.
    The Binary layer of gRPC will do all data operations like encoding and it also uses protobuf as an intermediator between client and server, improving performance.
    It is also used for communication between multiple microservices efficiently

Different scenarios in which we use gRPC

    When we use microservice architecture and we use that for internal communication from one or more servers
    It is also useful when performance is on high priority and low latency
    When we require duplex communication between services with different types of data

Pros and Cons of gRPC
Pros
    High Performance - faster than REST and SOAP
    Lightweight Message - gRPC Message is more lightweight than other types like JSON
    High Efficiency than other type
    Duplex data streaming

Cons
    Limited Browser Support
    It uses Binary Data due that it’s not easily readable like JSON and XML

Implementation of gRPC

Step 1
Create a new gRPC project

Step 2
Configure the project


Step 3
Provide additional information

Step 4
Project Structure

Here you will see the default project structure with greet proto and Greeter Service

syntax = "proto3";

option csharp_namespace = "GrpcService";

package greet;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}

    Protobuf is Interface Definition Language (IDL) for gRPC and uses to store data and contracts between clients and servers
    Line No 1 to 5 as you see there we declare types of protobuf syntax, namespace, and package
    Line No 7 to 11 there is a Unary Service Definition which takes a single request and the server sends back the response and it works as a function
    There are also many service definitions like Server Streaming, Client Streaming, and Bidirectional streaming RPCs. If you want to learn more about then read the grpc.io document
    Later on, there is a request and response function in order

using Grpc.Core;
namespace GrpcService.Services
{
    public class GreeterService : Greeter.GreeterBase
    {
        private readonly ILogger<GreeterService> _logger;
        public GreeterService(ILogger<GreeterService> logger)
        {
            _logger = logger;
        }

        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name
            });
        }
    }
}


This is the Greeter Service which inherits from Greeter.GreeterBase and inside that we inject ILogger and there is one method which takes the message to send by the client and send back as a response

(Note: Build Application whenever you add a new proto and Service)

Also, make sure the proto property is configured properly as shown below

Here we can see Build Action Protobuf compiler and gRPC Stub Classes are Server only

Let’s create a Client Console Application

Step 1
Create a new console application


Step 2
configure your project

Step 3
provide additional information

Step 4

Copy greet proto file from the server and change the gRPC Stub Classes to Client only and build it

Step 5
Add client code inside the Program file

using Grpc.Net.Client;
using GrpcService;
using GrpcService.Protos;
var message = new HelloRequest {
    Name = "Jaydeep"
};
var channel = GrpcChannel.ForAddress("http://localhost:5045");
var client = new Greeter.GreeterClient(channel);
var srerveReply = await client.SayHelloAsync(message);
Console.WriteLine(srerveReply.Message);
Console.ReadLine();

    Here we create a channel after configuring the server’s URL and create a channel
    Later on, call the method after passing a parameter to the server and print the response message inside the console

Step 6
Finally, run your code after configuring your both project as startup projects in proper order

This is the final output
Now we are going to add our new proto and service related to the product application in that we pass the product id from the client and the server will send the particular product details back to the client

Step 1
Create a new product proto file and change properties to Protobuf compiler and Servers only after that build the project
syntax = "proto3";

option csharp_namespace = "GrpcService.Protos";

package product;

service Product {
    rpc GetProductsInformation (GetProductDetail) returns (ProductModel);
}

message GetProductDetail{
    int32 productId = 1;
}

message ProductModel{
    string productName = 1;
    string productDescription = 2;
    int32 productPrice = 3;
    int32 productStock = 4;
}

Here you can see, that we define service which takes product id as a parameter and send product details to the client

Step 2
Next, create a Product Service
using Grpc.Core;
using GrpcService.Protos;

namespace GrpcService.Services
{
    public class ProductService : Product.ProductBase
    {
        private readonly ILogger<ProductService> _logger;
        public ProductService(ILogger<ProductService> logger)
        {
            _logger = logger;
        }

        public override Task<ProductModel> GetProductsInformation(GetProductDetail request, ServerCallContext context)
        {
            ProductModel productDetail = new ProductModel();
            if (request.ProductId == 1)
            {
                productDetail.ProductName = "Samsung TV";
                productDetail.ProductDescription = "Smart TV";
                productDetail.ProductPrice = 35000;
                productDetail.ProductStock = 10;
            }
            else if (request.ProductId == 2)
            {
                productDetail.ProductName = "HP Laptop";
                productDetail.ProductDescription = "HP Pavilion";
                productDetail.ProductPrice = 55000;
                productDetail.ProductStock = 20;
            }
            else if (request.ProductId == 3)
            {
                productDetail.ProductName = "IPhone";
                productDetail.ProductDescription = "IPhone 12";
                productDetail.ProductPrice = 65000;
                productDetail.ProductStock = 30;
            }

            return Task.FromResult(productDetail);
        }
    }
}


Here you can see we create Product Service which inherits from Product.ProductBase and after that, we inject ILogger of type Product Service and create one method and inside that whatever product details client wants that we check and return as a response corresponding to the particular product which is sent by the client

Step 3
Map the Product Service inside the Program class
using GrpcService.Services;
var builder = WebApplication.CreateBuilder(args);
// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
// Add services to the container.
builder.Services.AddGrpc();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapGrpcService < GreeterService > ();
app.MapGrpcService < ProductService > ();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();


Step 4
Build Server Project

Step 5
Copy the Product protobuf file inside the client and change the properties to the client only
syntax = "proto3";
option csharp_namespace = "GrpcService.Protos";
package product;
service Product {
    rpc GetProductsInformation(GetProductDetail) returns(ProductModel);
}
message GetProductDetail {
    int32 productId = 1;
}
message ProductModel {
    string productName = 1;
    string productDescription = 2;
    int32 productPrice = 3;
    int32 productStock = 4;
}

Step 6
Next, add client functionality inside the Program class
using Grpc.Net.Client;
using GrpcService;
using GrpcService.Protos;
//var message = new HelloRequest { Name = "Jaydeep" };
//var channel = GrpcChannel.ForAddress("http://localhost:5045");
//var client = new Greeter.GreeterClient(channel);
//var srerveReply = await client.SayHelloAsync(message);
//Console.WriteLine(srerveReply.Message);
//Console.ReadLine();
var channel = GrpcChannel.ForAddress("http://localhost:5045");
var client = new Product.ProductClient(channel);
var product = new GetProductDetail {
    ProductId = 3
};
var serverReply = await client.GetProductsInformationAsync(product);
Console.WriteLine($ "{serverReply.ProductName} | {serverReply.ProductDescription} | {serverReply.ProductPrice} | {serverReply.ProductStock}");
Console.ReadLine();


Here we create a channel after configuring the server’s URL and after that create a model object with a user id and pass it to the method which gets the product details as a response. Finally, we just print the product details in the console

Step 7

Build and run your both projects after setting up as a startup project

So, this is the output of our service.

In this section, we just understand the basic working of gRPC with Product Application. But, in real-time scenarios, there are many ways to implement that like using Background service and some different gRPC streaming techniques that we discussed in upcoming articles

Conclusion
In this article, we discussed gRPC, Pros, and Cons of gRPC. Also, the scenarios in which gRPC plays an important role and step-by-step implementation using .NET Core 6

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



European ASP.NET Core Hosting - HostForLIFE :: Feature Flag In .NET 6.0

clock September 21, 2022 09:22 by author Peter

Feature flags also referred to as Feature Toggles are a powerful way to modify applications behavior without making any changes in the code. It allows features to be turned on and off dynamically.

In this tutorial, we are going to look at the below,

    Feature Management Library in .NET
    How can we use Feature Management Library in ASP.NET Core applications
    How to add a simple Boolean feature flag.
    Advanced Feature Flag filters.
    Custom Feature Flag Filter.

Feature Management Library

The .NET Core Feature Management Library extends the framework with comprehensive feature flag support. These libraries are built on top of the .NET Core Configuration system. Any .NET Core configuration provider can act as a backbone for feature flags.

The definition which I copied from GitHub.

“The Microsoft.FeatureManagement library enables developers to use feature flags and dynamic features inside of their applications. Feature flags can be used to turn features on or off dynamically. Developers can use feature flags in simple use cases like conditional statements to more advanced scenarios like conditionally adding routes or MVC filters. Dynamic features can be used to select different variants of a feature's configuration. This enables the possibility of using one version of a feature for one set of users, and another version of the feature for the remaining users”
Use Feature Management Library in ASP.NET Core

The tools which I have used for this tutorial are below

    VS 2022 Community Edition Preview Version 17.4 - Preview 2.0
    .NET 6.0
    Web API
    Swagger

Without any further delay, let us create an ASP.NET Core API project. Then add the below package from the NuGet.

Microsoft.FeatureManagement.AspNetCore
Add a simple Boolean Feature Flag

As we have successfully installed the NuGet package, let us go ahead and Feature flag into the Controller “WeatherForecastController”
To add the Feature flag, we need to inject an interface called “IFeatureManager”. This interface belongs to the NuGet package “Microsoft.FeatureManagement.AspNetCore”

The WeatherForecastController constructor would be looks like below
private readonly IFeatureManager _featureManager;
public WeatherForecastController(ILogger < WeatherForecastController > logger, IFeatureManager featureManager) {
    _logger = logger;
    this._featureManager = featureManager;
}


Now, we are going to add the Feature flag in the endpoint method –“GetWeatherForecast”
By default, this method returns list of WeatherForecast with properties Date, TemperatureC, and Summary. The GetWeatherForecast method given below
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable < WeatherForecast > Get() {
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast {
        Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    }).ToArray();
}

Let us go ahead and add a new feature called “TemperatureF” into the Model – “WeatherForecast”
public class WeatherForecast {
    public DateTime Date {
        get;
        set;
    }
    public int TemperatureC {
        get;
        set;
    }
    public string ? Summary {
        get;
        set;
    }
    //New Feature
    public int TemperatureF {
        get;
        set;
    }
}


While building this feature, we need to feature flag this new property as we are not sure when this is going to be deployed into production. So, we need to provide an ability to on and off this feature as we develop it. Let us start using the Feature Manager to determine this.

The updated GetWeatherForecast endpoint method is given below
[HttpGet(Name = "GetWeatherForecast")]
public async Task < IEnumerable < WeatherForecast >> Get() {
    //Feature Flag - this needs to be configure in AppSettings.json
    var isTemperatureFEnabled = await _featureManager.IsEnabledAsync("TemperatureF");
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast {
        Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            //check whether the Feature Flag is enabled
            TemperatureF = isTemperatureFEnabled ? (int)(Random.Shared.Next(-20, 55) * 1.8) + 32 : null,
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    }).ToArray();
}

Now we need to configure this Feature flag in any of the configuration source. In this tutorial, we are going to configure in AppSettings.json
"FeatureManagement": {
    "TemperatureF": true
}


The next step – we need to register the IFeatureManagement interface into dependency container. Now let us go to Program.cs and add the below line of code
builder.Services.AddFeatureManagement();

We have completed the configuration. Let us go ahead and execute the Endpoint GetWeatherForecast, we will get the below response
[{
        "date": "2022-09-17T23:08:43.4688307-04:00",
        "temperatureC": 25,
        "summary": "Balmy",
        "temperatureF": 37
    }, {
        "date": "2022-09-18T23:08:43.4693542-04:00",
        "temperatureC": 44,
        "summary": "Warm",
        "temperatureF": 44
    }, {
        "date": "2022-09-19T23:08:43.4693578-04:00",
        "temperatureC": -14,
        "summary": "Mild",
        "temperatureF": 91
    }

We can see that the new Feature “TemparatureF” has been returned.

Now let us go to disable this feature in AppSettings.json and execute the endpoint
"FeatureManagement": {
    "TemperatureF": false
}


Now execute the endpoint and look at the response.
[{
        "date": "2022-09-17T23:11:51.823846-04:00",
        "temperatureC": -17,
        "summary": "Hot",
        "temperatureF": null
    }, {
        "date": "2022-09-18T23:11:51.8238573-04:00",
        "temperatureC": 28,
        "summary": "Chilly",
        "temperatureF": null
    }, {
        "date": "2022-09-19T23:11:51.8238577-04:00",
        "temperatureC": 29,
        "summary": "Freezing",
        "temperatureF": null
    }


The TemperatureF has been displayed with “null” value. This property can be turned off altogether when it is null. To do that, go to Startup.cs and add the below lines of code.
//Ignore Null Values
builder.Services.AddControllers().AddJsonOptions(option => {
    option.JsonSerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});


Now, let us go ahead and execute the code and see what the response would be.
[{
        "date": "2022-09-17T23:21:36.5978036-04:00",
        "temperatureC": 20,
        "summary": "Chilly"
    }, {
        "date": "2022-09-18T23:21:36.5981674-04:00",
        "temperatureC": 19,
        "summary": "Freezing"
    }, {
        "date": "2022-09-19T23:21:36.5981699-04:00",
        "temperatureC": -19,
        "summary": "Bracing"
    }


TemperatureF property is not visible

Feature Flag Filters
Let us go ahead and add a new endpoint as below
[HttpGet("newfeature")]
public async Task < IEnumerable < WeatherForecast >> GetNewEndPoint() {
    //Feature Flag - this needs to be configure in AppSettings.json
    var isTemperatureFEnabled = await _featureManager.IsEnabledAsync("TemperatureF");
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast {
        Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            //check whether the Feature Flag is enabled
            TemperatureF = isTemperatureFEnabled ? (int)(Random.Shared.Next(-20, 55) * 1.8) + 32 : null,
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    }).ToArray();
}

We are going to add feature flag to this method. Let us see how this can be achieved. Rather than we do an event check insider the method, we can simply add the below attribute at the method level to flag this method as a feature.
[FeatureGate("NewFeature")] – here “NewFeature” is the value of the feature flag

This attribute belongs to the namespace – “Microsoft.FeatureManagement.Mvc”

The updated method looks like below,
[HttpGet("newfeature")]
[FeatureGate("NewFeature")]
public async Task < IEnumerable < WeatherForecast >> GetNewFeature() {
    //Feature Flag - this needs to be configure in AppSettings.json
    var isTemperatureFEnabled = await _featureManager.IsEnabledAsync("TemperatureF");
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast {
        Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            //check whether the Feature Flag is enabled
            TemperatureF = isTemperatureFEnabled ? (int)(Random.Shared.Next(-20, 55) * 1.8) + 32 : null,
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    }).ToArray();
}

Now, let us go ahead and add this feature flag in AppSettings.json
"FeatureManagement": {
    "TemperatureF": false,
    "NewFeature": true
}


Let us go ahead and execute the endpoint
WeatherForecast/newfeature
[{
        "date": "2022-09-19T00:19:43.2623594-04:00",
        "temperatureC": 1,
        "summary": "Freezing"
    }, {
        "date": "2022-09-20T00:19:43.2651919-04:00",
        "temperatureC": 7,
        "summary": "Cool"
    }, {
        "date": "2022-09-21T00:19:43.2652001-04:00",
        "temperatureC": 26,
        "summary": "Scorching"
    }

Now update the “NewFeature” flag in AppSettings.json as below,
"FeatureManagement": {
    "TemperatureF": false,
    "NewFeature": false
} {
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
    "title": "Not Found",
    "status": 404,
    "traceId": "00-4a24d9885877e1d886949c330cd2dd16-35b5ed50d738cba3-00"
}


We got the 404 error.

In the upcoming tutorial, I will be explaining Advanced Feature Flag and Custom Feature flag.

Thank you for reading my article.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



European ASP.NET Core Hosting :: Create Window Service In .NET Core

clock September 12, 2022 10:40 by author Peter

In this article, we will learn how to create a window service with .NET Core using Quartz Cron expression. And host an application as a windows service on the server machine.

Step 1
To begin, make a project with the.NET core console application template.

Step 2
Install Required packages for Host Builder using Nuget Package Manager.

Host Builder Introduction
Host Builder is the new “generic” Host which enables developers to easily set up cross-cutting concerns such as logging, configuration, and dependency injection for non-web-focused applications. The team realized that having the host tied to the concern of HTTP was perhaps not an ideal solution since many of these are common requirements in other application types.

An example of where this could be used is in a console application that needs to run background processing tasks, perhaps handling messages on a queue for example. These types of services are now pretty common in a cloud-native, container-based architecture.

Step 3
Install the required package for Quartz using Nuget Package Manager.

Also, install Quartz using the Package Manager Console as below:
NuGet\Install-Package Quartz -Version 3.4.0

Step 4
Once package install is completed we can write code for the scheduler which will run after a specific amount of time. Please find below sample code to write in program.cs file to write log at specific amount of time.
class Program {
    static async Task Main(string[] args) {
        IHost Host = CreateHostBuilder(args).Build();
        await Host.RunAsync();
    }
    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureServices(services => {
        ConfigureQuartzService(services);
        services.AddScoped < ITaskLogTime, TaskLogTime > ();
    });
    private static void ConfigureQuartzService(IServiceCollection services) {
        // Add the required Quartz.NET services
        services.AddQuartz(q => {
            // Use a Scoped container to create jobs.
            q.UseMicrosoftDependencyInjectionJobFactory();
            // Create a "key" for the job
            var jobKey = new JobKey("Task1");
            // Register the job with the DI container
            q.AddJob < Task1 > (opts => opts.WithIdentity(jobKey));
            // Create a trigger for the job
            q.AddTrigger(opts => opts.ForJob(jobKey) // link to the Task1
                .WithIdentity("Task1-trigger") // give the trigger a unique name
                .WithCronSchedule("0/5 * * * * ?")); // run every 5 seconds
        });
        // Add the Quartz.NET hosted service
        services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
    }
}


Cron Trigger Introduction
As we utilize quartz we can use a cron trigger. Cron is nothing but it is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron.

As for POC added cron trigger for 5 seconds with the below expression

"0/5 * * * * ?"

Create a trigger and added to quartz like the below code,
private static void ConfigureQuartzService(IServiceCollection services) {
    // Add the required Quartz.NET services
    services.AddQuartz(q => {
        // Use a Scoped container to create jobs.
        q.UseMicrosoftDependencyInjectionJobFactory();
        // Create a "key" for the job
        var jobKey = new JobKey("Task1");
        // Register the job with the DI container
        q.AddJob < Task1 > (opts => opts.WithIdentity(jobKey));
        // Create a trigger for the job
        q.AddTrigger(opts => opts.ForJob(jobKey) // link to the Task1
            .WithIdentity("Task1-trigger") // give the trigger a unique name
            .WithCronSchedule("0/5 * * * * ?")); // run every 5 seconds
    });
    // Add the Quartz.NET hosted service
    services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
}


Step 5
Build and Run the application using exe file. Please find attached the full source code which will write the log to log.txt file for a current time after every 5 sec as per our cron expression.

Start exe


Check Log folder for the result

Step 6
As we run our application manually using exe file. Now sit back and think on server do we run our application like this is a good practice. Not a good idea. So below is the solution.

Host dotnet core console application as a window service

Please follow the below steps to host a console application as a window service.

Required Code changes
To support the window service console application, need to allow UseWindowsService in

CreateHostBuilder met method.

To use window service need to install the NuGet package
‘Microsoft.AspNetCore.Hosting.WindowsServices’

To install it by command use “Install-Package Microsoft.AspNetCore.Hosting.WindowsServices - Version”

Note
Need to install the package as per your .NET core version compatibility.

Change in Program.cs file.
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).UseWindowsService().ConfigureServices(services => {
    ConfigureQuartzService(services);
    services.AddScoped < ITaskLogTime, TaskLogTime > ();
});


Once code changes are done Publish the application in Release mode.

After publishing the application, open Command Prompt in administrator mode.

Command To Create Service > SC CREATE "ServiceName" binpath="D:\Published \application\path\application.exe"

Command To Remove Service > SC DELETE ServiceName

Once the service is created. Open press win + R > Type services.msc > Enter.

Service should be visible here by the name you have entered during creation.

Start and stop service from here and change the configuration as per requirement. (I.e. login by another user, automatic or manual)

Advantage
    The server admin is not required to run an exe file on the server manually.
    Server admin can set different credentials as per requirement.
    It can be set as an automatic or manual start. so whenever the server is down and restarted no need to check it will automatically start service as per configuration.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



European ASP.NET Core Hosting :: JWT Token Creation, Authentication And Authorization In ASP.NET Core 6.0 With Postman

clock September 6, 2022 07:38 by author Peter

In this article, I will explain how to create the JWT token and how to Authenticate and Authorize it in very simple steps. We will follow the below steps to JWT token creation, authentication and authorization.

  • ASP.Net Core API Application
  • Add required packages.
  • Add Key, Issuer and Audience in appsettings.cs
  • Register JWT Token for Authentication in Startup.cs file.
  • Create Models (UserLogin, UserModel and UserConstant)
  • Create Login API Controller (Authenticate user and generate token)
  • Create User API Controller to authorize user role.
  • Test the API endpoint in Postman with Token.

1. Add ASP.Net Core API Application
Open visual studio 2022 click on create new project --> Select ASP.Net Core Web API --> Next

Give desired project and solution name --> Next --> select framework .Net 6.0 --> Create

2. Add Nuget Packages
Add the following packages from nuget package manager.
    Microsoft.AspNetCore.Authentication.JwtBearer
    Microsoft.IdentityModel.Tokens
    System.IdentityModel.Tokens.Jwt

 


3. Add setting in appsetting.json
Open appsetting.json and add following Key, Issuer and Audience

* To generate the random key use
https://www.random.org/strings

* For issuer and audience local URL follow the below steps
Project properties --> Debug --> General --> Open Debug Launch Profile UI


Select IIS Express and pick the App URL

"Jwt": {
    "Key": "ACDt1vR3lXToPQ1g3MyN", //Generate random String from https://www.random.org/strings
    "Issuer": "http://localhost:28747/", //Project Property-> Debug-> IIS-->App URL (you can local host url as well)
    "Audience": "http://localhost:28747/"
  },

4. Register JWT token for Authentication in Program.cs file

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//JWT Authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => {
    options.TokenValidationParameters = new TokenValidationParameters {
        ValidateIssuer = true,
            ValidateAudience = true,
            ValidateLifetime = true,
            ValidateIssuerSigningKey = true,
            ValidIssuer = builder.Configuration["Jwt:Issuer"],
            ValidAudience = builder.Configuration["Jwt:Audience"],
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
    };
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

5. Create Models (UserLogin, UserModel and UserConstant)
Add a new folder with Models name and create UserLogin, UserModel and UserConstant classes.

namespace JWTLoginAuthenticationAuthorization.Models
{
    public class UserModel
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public string Role { get; set; }
    }
}

namespace JWTLoginAuthenticationAuthorization.Models
{
    public class UserLogin
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

namespace JWTLoginAuthenticationAuthorization.Models
{
    // We are not taking data from data base so we get data from constant
    public class UserConstants
    {
        public static List<UserModel> Users = new()
            {
                    new UserModel(){ Username="naeem",Password="naeem_admin",Role="Admin"}
            };
    }
}

6. Create LoginAPI Controller (Authenticate user and generate token)
Add a new Empty API controller name “LoginController” in controller folder.

 

Here creates one Post Action method for Login and two methods for Authenticating the user credentials and Generate the token (if user is authenticated).

using JWTLoginAuthenticationAuthorization.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace JWTLoginAuthenticationAuthorization.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly IConfiguration _config;
        public LoginController(IConfiguration config)
        {
            _config = config;
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login([FromBody] UserLogin userLogin)
        {
            var user = Authenticate(userLogin);
            if (user != null)
            {
                var token = GenerateToken(user);
                return Ok(token);
            }

            return NotFound("user not found");
        }

        // To generate token
        private string GenerateToken(UserModel user)
        {
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier,user.Username),
                new Claim(ClaimTypes.Role,user.Role)
            };
            var token = new JwtSecurityToken(_config["Jwt:Issuer"],
                _config["Jwt:Audience"],
                claims,
                expires: DateTime.Now.AddMinutes(15),
                signingCredentials: credentials);


            return new JwtSecurityTokenHandler().WriteToken(token);

        }

        //To authenticate user
        private UserModel Authenticate(UserLogin userLogin)
        {
            var currentUser = UserConstants.Users.FirstOrDefault(x => x.Username.ToLower() ==
                userLogin.Username.ToLower() && x.Password == userLogin.Password);
            if (currentUser != null)
            {
                return currentUser;
            }
            return null;
        }
    }
}

7. Create User API Controller to authorize user role
Add new empty API controller named “UserController.cs” in controller folder.
Here we will authorize the endpoint on the behalf of role.

namespace JWTLoginAuthenticationAuthorization.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        //For admin Only
        [HttpGet]
        [Route("Admins")]
        [Authorize(Roles = "Admin")]
        public IActionResult AdminEndPoint()
        {
            var currentUser = GetCurrentUser();
            return Ok($"Hi you are an {currentUser.Role}");
        }
        private UserModel GetCurrentUser()
        {
            var identity = HttpContext.User.Identity as ClaimsIdentity;
            if (identity != null)
            {
                var userClaims = identity.Claims;
                return new UserModel
                {
                    Username = userClaims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value,
                    Role = userClaims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value
                };
            }
            return null;
        }
    }
}

8. Test the API endpoint in Postman with Token
Run the application and copy the URL domain from the browser.
Now open the Postman, give the URL with correct API route and select post request --> Body --> Json --> give the value of Username and Password
.

After clicking on send button we will get the JWT token in response.

Now copy this token and add a new Get request in postman and add the JWT token Authorization Tab --> Select Bearer --> Insert token and click on send button to test the authorization with given token. If the token is not valid token then we will get 401 Error otherwise will get the bolow result.

So we created the token and did the authentication on the behalf of username and password then check the user authorization.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.




European ASP.NET Core Hosting :: Web API With ASP.NET 6 And MySQL

clock September 5, 2022 10:30 by author Peter

In this tutorial article, we will learn how to build a web API from ASP.NET 6 to handle CRUD operations with a database in MySQL.

Resources required
To follow this article step by step or run the included demo, it is necessary to have the following tools in operation:
    MySQL.
    .NET 6 SDK.
    Visual Studio 2019/2022.
    The web development workload and ASP.NET for Visual Studio 2019/2022.

The process to follow,
In the tutorial, we will have three important parts:
    Review the database we are going to use.
    Establish database access from ASP.NET through the Entity Framework.
    Set the handlers and their methods for the web service.

As a case study for this tutorial, user data will be handled through CRUD (Create, Read, Update, and Delete) operations.

1. The database for the application domain.
The database that we will use in this example is made up of a single table called: User, with the attributes: Id, FirstName, LastName, Username, Password, and EnrrollmentDate; in MySQL.

SQL statements for the creation of the User table are as follows:
CREATE TABLE `user` (
  `Id` INT NOT NULL PRIMARY KEY,
  `FirstName` VARCHAR(45) NOT NULL,
  `LastName` VARCHAR(45) NOT NULL,
  `Username` VARCHAR(45) NOT NULL,
  `Password` VARCHAR(45) NOT NULL,
  `EnrollmentDate` datetime NOT NULL
);


Very well, with the database established, we can already start with the implementation of our first project for the development of API Rest services.

SQL statements for the creation of the User table are as follows:
CREATE TABLE `user` (
  `Id` INT NOT NULL PRIMARY KEY,
  `FirstName` VARCHAR(45) NOT NULL,
  `LastName` VARCHAR(45) NOT NULL,
  `Username` VARCHAR(45) NOT NULL,
  `Password` VARCHAR(45) NOT NULL,
  `EnrollmentDate` datetime NOT NULL
);

Very well, with the database established, we can already start with the implementation of our first project for the development of API Rest services.

2. Establish database access from ASP.NET through Entity Framework.
ASP.NET 6 Web API project.

In Visual Studio, the first thing we'll do is create a new project of the type ASP.NET Core Web API:

Then, in the following steps, we can specify the Framework.

With this project, we'll create access to the database and implement a corresponding controller to work with that data and provide the web API.

Database access with Entity Framework.
To establish the entities through classes and the connection of the database, we can use the Database First approach of the Entity Framework, which allows us to scaffold from the database to the project, that is, generate classes automatically according to the entities established in the database and the connection in the project.

For this purpose, it's necessary to install three NuGet packages,
    Microsoft.EntityFrameworkCore.Design
    Microsoft.EntityFrameworkCore.Tools
    MySql.EntityFrameworkCore

In case you are working with SQL Server, the NuGet package to install will be: Microsoft.EntityFrameworkCore.SQLServer.

Note: To find the admin center of NuGet packages, we can go to the option: Menu -> Project -> Manage NuGet Packages...

With the installation of these NuGet packages, we'll now open the package manager console to write a command that will allow us to perform scaffolding from the database:


Command
Scaffold-DbContext "server=servername;port=portnumber;user=username;password=pass;database=databasename" MySql.EntityFrameworkCore -OutputDir Entities -f

The result is as follows:



Here, the User class is defined as follows,
public partial class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public DateTime EnrollmentDate { get; set; }
}

And the DBContext, which has the configuration with the database, whose main method OnConfiguring will look something like this,
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
  if (!optionsBuilder.IsConfigured)
  { optionsBuilder.UseMySQL("server=localhost;port=3306;user=root;password=;database=database");
  }
}


Now, it's not the most appropriate that the connection string to the database is specified in the OnConfiguring method. For this, within our project, we can find the appsettings.json file, in which we can define this configuration,
"AllowedHosts": "*",
"ConnectionStrings": {
  "DefaultConnection": "server=localhost;port=3306;user=root;password=;database=demo;"
}


Then, in the Program class, we'll add as a service to the DBContext, and then we must reference the DefaultConnection the property specified in the appsettings.json file:
builder.Services.AddEntityFrameworkMySQL().AddDbContext < DBContext > (options => {
    options.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection"));
  });
});

In this case, returning to the class of the DBContext, we delete the connection string specified in the OnConfiguring method.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{}


With these steps, we have already ready the connection and the necessary configurations to work with the database in ASP.NET with the help of Entity Framework.


3. Set the controllers and their methods for the web service.
In order to transport the data between the processes for the management of the database and the processes for working with web services, it's advisable to establish DTO classes for each entity of the project, in this case, a DTO for the entity User.

To do this, we'll create a new folder within the project called DTO and create a class called UserDTO, whose attributes will be the same as the User class defined in the Entities section above:
public class UserDTO
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public DateTime EnrollmentDate { get; set; }
}


Controllers for the Web API.

Now what we'll do is add the controllers, in this case, the controller for the user, which will allow establishing methods to perform CRUD operations on the tables of the database and expose them through the Web API. On the Controllers folder, we'll add a controller called UserController:

The definition of the class and its constructor will look like this:
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
    private readonly DBContext DBContext;

    public UserController( DBContext DBContext)
    {
        this.DBContext = DBContext;
    }
    ...
}

Now, the goal is to perform CRUD operations. In this sense, we'll use methods to access the information (Get), insert data (Post), modify (Put), and delete a record (Delete).

The following is the final code for each of the methods:

A. Get the list of all registered users.
[HttpGet("GetUsers")]
public async Task<ActionResult<List<UserDTO>>> Get()
{
    var List = await DBContext.User.Select(
        s => new UserDTO
        {
            Id = s.Id,
            FirstName = s.FirstName,
            LastName = s.LastName,
            Username = s.Username,
            Password = s.Password,
            EnrollmentDate = s.EnrollmentDate
        }
    ).ToListAsync();

    if (List.Count < 0)
    {
        return NotFound();
    }
    else
    {
        return List;
    }
}


B. Obtain the data of a specific user according to their Id.
[HttpGet("GetUserById")]
public async Task < ActionResult < UserDTO >> GetUserById(int Id) {
    UserDTO User = await DBContext.User.Select(s => new UserDTO {
        Id = s.Id,
            FirstName = s.FirstName,
            LastName = s.LastName,
            Username = s.Username,
            Password = s.Password,
            EnrollmentDate = s.EnrollmentDate
    }).FirstOrDefaultAsync(s => s.Id == Id);
    if (User == null) {
        return NotFound();
    } else {
        return User;
    }
}


C. Insert a new user.
[HttpPost("InsertUser")]
public async Task < HttpStatusCode > InsertUser(UserDTO User) {
    var entity = new User() {
        FirstName = User.FirstName,
            LastName = User.LastName,
            Username = User.Username,
            Password = User.Password,
            EnrollmentDate = User.EnrollmentDate
    };
    DBContext.User.Add(entity);
    await DBContext.SaveChangesAsync();
    return HttpStatusCode.Created;
}


D. Update the data of a specific user.
[HttpPut("UpdateUser")]
public async Task < HttpStatusCode > UpdateUser(UserDTO User) {
    var entity = await DBContext.User.FirstOrDefaultAsync(s => s.Id == User.Id);
    entity.FirstName = User.FirstName;
    entity.LastName = User.LastName;
    entity.Username = User.Username;
    entity.Password = User.Password;
    entity.EnrollmentDate = User.EnrollmentDate;
    await DBContext.SaveChangesAsync();
    return HttpStatusCode.OK;
}

E. Delete a user based on their Id.
[HttpDelete("DeleteUser/{Id}")]
public async Task < HttpStatusCode > DeleteUser(int Id) {
    var entity = new User() {
        Id = Id
    };
    DBContext.User.Attach(entity);
    DBContext.User.Remove(entity);
    await DBContext.SaveChangesAsync();
    return HttpStatusCode.OK;
}


With these methods and the steps followed up to this point, the web service is ready to run.

Test the implemented web API
To test the implemented API we can use Swagger UI, a visual tool that allows us to interact with the methods of our service, and that in turn is already integrated into our ASP.NET 6 project.

For testing, we need to build and run the application:

Next, we can see the Swagger interface so that we can perform the corresponding tests according to the methods defined in our controller and in an interactive way:




As this is a RestFul service, we may use any other program or application to consume these services. For example, here we can see a call to the GetUsers method from the Postman tool:


HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



European ASP.NET SignalR Hosting - HostForLIFE :: SignalR Best Practices

clock September 2, 2022 07:34 by author Peter

To achieve real-time messaging, earlier we used long-polling and server-sent events. We can achieve real-time messaging in .Net Core using SignalR. It uses WebSocket protocols for real-time messaging to make communication bidirectional from client to server. It delivers information in real time, without the need to refresh the app screen.

Protocol
SignalR supports both WebSocket (WS) and WebSocket Secure (WSS) protocols. The application will be connected to the SignalR hub using these protocols. Based on the needs we can use the corresponding protocols. We can create a secure socket connection using WSS protocol like HTTPS. If the application connects from the external world, then we can use WSS protocols the connection will become secure, otherwise, we can use the WS protocol. WS protocol uses port 80 for connectivity. WSS protocol uses port 443 for connectivity. We need to use port 5000 while implementing the SignalR service in the OpenShift environment.

Authentication using headers
We need to enable the authentication for SignalR connection from the client application based on the business needs. If the application needs authentication, then the application should use the WSS protocol. We can enable authentication in multiple ways. Passing the bearer token information in Cookie. But it's not recommended way. We can pass the Bearer token in the authorization header from some of the .Net Client applications. Android and Html5 apps do not support passing the Bearer token in the authorized header. We need to pass the token information in the query parameters using the access_token keyword.

Hub Connectivity

The application will connect to the SignalR hub for any real-time message communication. There are two types of connectivity. The application can connect to the SignalR hub directly or it can connect to the hub via reverse proxy from some other services. If you want to manage all your services like web services, and SignalR services via a single endpoint then we can expose the hub via reverse proxy or we can expose a hub directly to the client application to connect.

Client vs Groups
SignalR hub will send messages to the specific clients by Connection id, or the list of clients connected via group using Group Id. It will be better to send the message to the group instead of a list of client IDs. It will avoid the multiple requests to send the message to each client from the hub.

On-Prem SignalR vs Azure SignalR service
When the client size increase, we need to manage all the connections in the Hub. We need to scale out the machines when we manage the SignalR Hub in our On-Prem Servers. Azure provides SignalR Hub Service, it will manage the client connection based on the plan. Clients will connect to Azure SignalR hub Service and backend we need to send the message using group Id or client Id to SignalR hub. It will deliver the message to the client connected to the hub.

Reconnections
ASP.NET SignalR automatically handles restarting the connection when a disconnection occurs. Reconnection behavior is often specific to each application. For this reason, ASP.NET Core SignalR doesn't provide a default automatic reconnection mechanism. For most common scenarios, a client only needs to reconnect to the hub when a connection is lost, or a connection attempt fails. To do this, the application can listen to these events and call the start method on the hub connection.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 

 

 



European ASP.NET Core Hosting :: Telemtery Converter Was Not Found Error

clock August 29, 2022 09:11 by author Peter

If you are using serilog 4.0, the latest GitHub documentation mentioned the depreciation of telemetry configuration active and future removal support of instrumentation Key.

They adjusted their namespaces, which cause the serilog configuration binding not able to find the type.

    GitHub

you have to replace instrumentation Key with 'connection string' and add correct namespace for telemetry converter in your appsetting.json.

"Serilog": {
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft": "Warning",
            "System": "Warning"
        }
    },
    "WriteTo": {
        "0": {
            "Name": "ApplicationInsights",
            "Args": {
                "connectionString": "",
                "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
            }
        },
        "1": {
            "Name": "Console"
        }
    },
    "Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId"],
    "Properties": {
        "ApplicationName": "<application_name>"
    },
    "AllowedHosts": "*"
},

You just need to paste connection string in Args section which you can find on application insights of azure monitor.

Program.cs

public static void Main(string[] args) {
    Activity.DefaultIdFormat = ActivityIdFormat.W3C;
    var env = Environment.GetEnvironmentVariable(Env);
    var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", true).AddJsonFile($ "appsettings.{env}.json", true).Build();
    //Initialize Logger
    Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(config).CreateLogger();
    try {
        Log.Information("application starting up...");
        CreateHostBuilder(args).Build().Run();
        Log.Information("application has started... running host");
    } catch (Exception ex) {
        Log.Fatal(ex, " application failed to start...");
    } finally {
        Log.CloseAndFlush();
    }
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).UseSerilog().ConfigureWebHostDefaults(webBuilder => {
    webBuilder.UseStartup < Startup > ();
});

Initialize Logger before calling CreateHostBuilder.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



European ASP.NET Core Hosting :: Upload/Download Bot Framework Composer Conversation/Chat

clock August 26, 2022 07:27 by author Peter

If you are working on Bot Framework Composer then challenging works you will find is about storing and retrieving conversation history.

Using Bot framework SDK, it is easy but complexity is there using composer.

While I was implementing, trust me I found very few articles and content to store/retrieve conversation.

Here I will mention all the points that can help you to preserve and download conversation history.

Pre-requisite

    Azure Portal access or Azurite

Step 1
Create a storage account in portal and create container

Step 2
Now, Follow some changes that are mentioned below,
<PackageReference Include="Microsoft.Bot.Builder.Azure.Blobs" Version="4.16.1" />

Add package to your project file,
public class Startup {
    private readonly BlobsTranscriptStore _blobTranscripts;
    public Startup(IConfiguration configuration) {
        Configuration = configuration;
        _blobTranscripts = new BlobsTranscriptStore(Configuration.GetValue < string > ("StorageConnectionString"), Configuration.GetValue < string > ("ContainerName"));
    }
    public void ConfigureServices(IServiceCollection services) {
        services.AddSingleton < ITranscriptStore > (_blobTranscripts);
        // Create the TranscriptLoggerMiddleware to log all activities to the TranscriptStore
        services.AddSingleton < IMiddleware, TranscriptLoggerMiddleware > (serviceProvider => {
            return new TranscriptLoggerMiddleware(_blobTranscripts);
        });
    }


Please note that add namespaces for above classes, I will leave up to you.

Now using above code, all your bot conversation starts storing on blob container.

Step 3
Now comes, the download of conversation part. Remember bot store JSON file for each of your message. so there will be lots of JSON get stored. To retrieve all transcript files of your conversation, you need channeled and conversationid.
var blobsTranscriptStore = new BlobsTranscriptStore("StorageConnectionString", "ContainerName");
string continutionToken = null;
List <IActivity> activitiesList = new List <IActivity> ();
do {
    var activities = await blobsTranscriptStore.GetTranscriptActivitiesAsync(ChannelId, ConversationId, continutionToken).ConfigureAwait(false);
    continutionToken = activities.ContinuationToken;
    activitiesList.AddRange(activities.Items);
} while (continutionToken != null);


Now you will find all conversation in activitiesList.

Step 4
Now you will find actual message in specific files by filtering using activity type.
foreach(Activity item in activities.Where(x => x.Type == ActivityTypes.Message).Cast<Activity> ()) {
    if (!string.IsNullOrEmpty(item.Text)) {
        Console.WriteLine($ "{item.Timestamp}   {item.From.Id} : {item.Text}");
    }
}


Now I will leave up to you whether you prepare text file or .csv file to store your conversation.

Definitely, you need to implement some of your logic here to get all messages. But I don't think it is too difficult.

Step 5

Now you can store this conversation file to blob storage again and allow it to download or you can directly allow user to download from here, the choice is your as per your requirement.

Hope the above article is helpful to you and you can customize this logic as my intention here is to provide you with core logic for this.

Keep learning, Keep Implementing.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.


 



European ASP.NET Core Hosting :: Creating QR Code In ASP.NET Core

clock August 24, 2022 09:37 by author Peter

Now a day's QR (Quick response) code becomes the internal part of our digital life. We will see a QR code on any E-tickets such as Stadium, air, bus, hotel booking. One of the best examples is India's UPI payment which is initiated by Indian PM Narendra Modi. You just have to scan the QR code to make the payment digital to the person’s bank account. This initiative is the very game changer since all the money comes to the bank and helped to stop the black money.

There are also many businesses generating the bar codes on E-Receipts to validate the authenticity of the receipt which can give the assurance that it's not been manipulated, which people did in the past simply editing and making false documents. There are many benefits using the QR code, the typical QR code will look like as follows.

Now in this article, we will learn how to generate a QR code using ASP.NET Core MVC. Let's learn step by step by creating an ASP.NET Core application.

Step 1: Create ASP.NET Core MVC Application
    Start then  All Programs and select "Microsoft Visual Studio 2019".
    Once the Visual Studio Opens, Then click on Continue Without Code.
    Then Go to Visual Studio Menu, click on File => New Project then choose ASP.NET Core Web App (Model-View-Controller) Project Template.
    Then define the project name, location of the project, Target Framework, then click on the create button.

The preceding steps will create the ASP.NET Core MVC application and solution explorer. It will look like as shown in the following image.

Delete the existing auto-generated code including controller and views for ease of understanding. We will add step by step while building the application.

Step 2: Add QRCoder Nuget Package Reference

The QRCoder is the open source pure C# library to generate the codes. The QRCoder supports the basic to custom and complex OR code generation, follow the following steps to add the Nuget package.

    Right click on the Solution Explorer, find Manage NuGet Package Manager and click on it
    After as shown into the image and type in search box "QRCoder"
    Select QRCoder as shown into the image
    Choose version of QRCoder library and click on install button

I hope you have followed the same steps and installed the QRCoder nuget package.

Step 3: Create the Model Class

Create the model class QRCodeModel by right clicking on the model folder to take the input text for QR code as shown in the following image.

Now open the QRCodeModel.cs class file and add the following code.

Step 4: Add the Controller
Create the Controller class by right clicking on the Controller folder as shown in the following image.

Now open the HomeController.cs file and add the following code.

using GeneratingQRCode.Models;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace GeneratingQRCode.Controllers
{
    public class HomeController : Controller
    {

        [HttpGet]
        public IActionResult CreateQRCode()
        {
            return View();
        }

        [HttpPost]
        public IActionResult CreateQRCode(QRCodeModel qRCode)
        {
            QRCodeGenerator QrGenerator = new QRCodeGenerator();
            QRCodeData QrCodeInfo = QrGenerator.CreateQrCode(qRCode.QRCodeText, QRCodeGenerator.ECCLevel.Q);
            QRCode QrCode = new QRCode(QrCodeInfo);
            Bitmap QrBitmap = QrCode.GetGraphic(60);
            byte[] BitmapArray = QrBitmap.BitmapToByteArray();
            string QrUri = string.Format("data:image/png;base64,{0}", Convert.ToBase64String(BitmapArray));
            ViewBag.QrCodeUri = QrUri;
            return View();
        }
    }

    //Extension method to convert Bitmap to Byte Array
    public static class BitmapExtension
    {
        public static byte[] BitmapToByteArray(this Bitmap bitmap)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Png);
                return ms.ToArray();
            }
        }
    }
}

Step 5: Create The View
Create the view with the name CreateQRCode using the model class QRCodeModel by right clicking on the view folder or right clicking in the controller class file as shown in the following image.

After clicking the Add button, it generates the view with the name CreateQRCode. Now open the CreateQRCode.cshtml file and replace the default generated code as follows:

CreateQRCode.cshtml

@model GeneratingQRCode.Models.QRCodeModel

@{
    ViewData["Title"] = "www.compilemode.com";
}
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="CreateQRCode">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="QRCodeText" class="control-label"></label>
                <input asp-for="QRCodeText" class="form-control" />
                <span asp-validation-for="QRCodeText" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Generate QR Code" class="btn btn-primary text-center" />
            </div>
            <div class="form-group">

                <img src="@ViewBag.QrCodeUri" class="img-thumbnail" />
            </div>
        </form>
    </div>
</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

In the preceding code you see the image tag src Uri assigned with ViewBag, which we have set from the HomeController class.

Step 6: Configure the Routing
We have modified the auto-generated files, so we have to set the default routing so that when you run the application, then the default view can start in the browser. Now open the Startup.cs and set the default routing as per our controller and the created view as shown in the following image.

After adding all the required files and configuration, the Solution explorer of our created ASP.NET core application will look like as follows.

Step 7: Run the Application
Now press Keyboard F5 or Visual Studio run button to run the application. After running the application, the following screen will be shown in the browser. Enter the QR code text and click on the Generate QR code button, then it will generate the following code as in the following screenshot.

Demo



Step 8: Read the QR Code
Open your mobile phone QR code reader application and scan it, it will show the following text which we have entered during the QR code creation.

I hope, from all the above examples, you have learned how to generate a QR code using the ASP.NET Core. If you like it, share it with your friends, subscribe to the blog and YouTube channel for more such articles.



European ASP.NET Core Hosting :: Creating Custom Attributes In .NET

clock August 15, 2022 10:20 by author Peter

Today we will look at creating custom attributes in .NET. Attributes provide metadata for the elements in our code. These can then be read via reflection and used to handle different conditional logic. The process to create and use them is simple. We will work through an example.

The Process

We will create a custom attribute and apply it to two different classes. Please note that this can be applied to other artifacts as well. I am using Visual Studio 2022 community edition for this article.

 

 

 

 

We now add a new class for the attribute.

 

Once created, add the below code to it.
namespace CustomAttribute {
    [AttributeUsage(AttributeTargets.Class)]
    public class MyDotNetAttribute: Attribute {
        public string ? DataReport {
            get;
            set;
        }
    }
}

Using the Custom Attribute
We will now create two classes and apply the custom attribute to them.
namespace CustomAttribute {
    [MyDotNet(DataReport = "EmployeeData")]
    public class Employee {
        public int Id {
            get;
            set;
        }
        public string ? FirstName {
            get;
            set;
        }
        public string ? LastName {
            get;
            set;
        }
    }
}
namespace CustomAttribute {
    [MyDotNet(DataReport = "EmployeeSalaryData")]
    public class Salary {
        public int EmployeeId {
            get;
            set;
        }
        public double EmployeeSalary {
            get;
            set;
        }
    }
}


Finally, we will update the “Program.cs” file as below:
// Create instances for the Employee and Salary classes
using CustomAttribute;
var employee = new Employee {
    Id = 1, FirstName = "John", LastName = "Doe"
};
var salary = new Salary {
    EmployeeId = 1, EmployeeSalary = 5000.00
};
// Now suppose we want to extract all classes with Employee related Data using reflection to generate some report etc.
var value = GetAttributeValue(employee);
Console.WriteLine(value);
value = GetAttributeValue(salary);
Console.WriteLine(value);
string GetAttributeValue < T > (T className) {
    var value = "Blank";
    var myAttributes = (MyDotNetAttribute[]) className.GetType().GetCustomAttributes(typeof(MyDotNetAttribute), true);
    if (myAttributes.Length > 0) {
        var myAttribute = myAttributes[0];
        value = myAttribute.DataReport;
    }
    return value;
}


In the above code, we see that we are extracting the attribute value using reflection. We can than use these values to implement our application logic as required.

Running the code gives us the below result:

In this article, we looked at creating custom attributes in .NET using C#. These will help us to add metadata to our artifacts like classes etc. We can than read these custom attributes in our application code using reflection and apply different logic depending upon the value that is extracted.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Month List

Tag cloud

Sign in