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 SignalR Hosting - HostForLIFE :: Using ASP.NET SignalR for Chat Application

clock September 27, 2022 10:42 by author Peter

Nowadays, chat applications have become pretty common. But still, the idea of how a client gets the message from the server without asking for it or in other words, without any event being fired, is remarkable. Such applications are known as real-time applications because they are responsible for live data exchange as used in multiplayer games, social media, or news forecasts. In real-time applications, the server pushes the message to connected clients instantly it becomes available to all connected clients. So, in this article, we will build a real-time chatting/twaddling application using SignalR. SignalR uses simple Web APIs to connect a server Hub to all of its clients basically, by creating server-to-client RPC (Remote Procedure Calls) that calls JavaScript methods in client browsers from the server and vice-versa.
 
Let's start building a twaddling application which would allow the user to share messages to all users as well as to individual users privately. Firstly, we start by creating a new web application project with MVC template in your visual studio. Then, you need to add the Microsoft.AspNet.SignalR NuGet package reference to the newly created project. After adding the reference, we have to register the SignalR Hub APIs by just calling the predefined method MapHubs() of RouteTableCollection at global.asax. But what does registering of hubs mean? It implies creating a route of the hub URLs and maps each to all the hubs in our project. Here is a demo of how API URLs are registered in the global.asax file,
    public class MvcApplication : System.Web.HttpApplication  
    {  
        protected void Application_Start()  
        {  
            BundleConfig.RegisterBundles(BundleTable.Bundles);  
            AreaRegistration.RegisterAllAreas();  
            RouteTable.Routes.MapHubs();  
            RouteConfig.RegisterRoutes(RouteTable.Routes);  
        }  
    }  


Now, we will create our own hub which will communicate with our client-side's javascript by just inheriting Microsoft.AspNet.SignalR.Hub class. This base class provides us with three properties and three virtuals methods, which would help us build our hub, namely,
    Clients: A collection of all the clients connected to the hub.
    Groups: A collection of groups of clients, mainly used for managing groups of clients.
    Context: Stores the details of that client which has called the server method.
    connected(): Triggered when a client joins the hub for the first time.
    OnDisconnected() : Triggered when a client leaves a hub.
    OnReconnected(): Triggered when a client joins the hub for the next time.

By using this properties and method, we create a hub named MasterHub where we will mainly have our broadcasting logics. So, now its time to define the client methods used in MasterHub but before that, we need to create a hub proxy by adding a script reference of ~/signalr/hubs and start that proxy by calling the predefined method $.connection.hub.start(). Here is an example how to create a hub proxy and declare all the client methods,
    <script src="~/signalr/hubs"></script>  
    <script type="text/javascript">  
    $(window).load(function () {  
        let hub = $.connection.masterHub;  
        //define all your client-side methods  
        hub.client.LogIn = function (parameters) {  
        //define the client side logics  
        }  
        //finally start the hub proxy  
        $.connection.hub.start().done(function () {  
            $.fn.TwaddlerLogIn(hub);  
        });  
    });  
    </script>  


Your application is ready for a run. The first step for the client is to get connected to the hub by entering his name.

    public class MasterHub : Hub  
    {  
        private static List<Twaddler> Twaddlers = new List<Twaddler>();  
      
        private static List<TwaddleDetails> Twaddles = new List<TwaddleDetails>();  
      
        public void OnConnected(string TwaddlerName)  
        {  
            if (!Twaddlers.Any(x => Equals(x.ConnectionId, Context.ConnectionId)))  
            {  
                var twaddler = new Twaddler()  
                {  
                    Name = TwaddlerName,  
                    ConnectionId = Context.ConnectionId  
                };  
      
                Twaddlers.Add(twaddler);  
                Clients.Caller.LogIn(twaddler, Twaddlers, Twaddles);  
      
                //broadcast the new twaddler to all twaddlers  
                Clients.AllExcept(Context.ConnectionId).TwaddlerLogIn(twaddler);  
            }  
        }  


Then, the client could send a message to all the clients in the hub, globally.
 

    public void BroadcastTwaddle(TwaddleDetails twaddle)  
    {  
        Twaddles.Add(twaddle);  
        //broadcast the new twaddle to all twaddlers  
        Clients.All.BroadcastTwaddle(twaddle);  
    }  

The client could also send a private message to any specific hub, privately.

    public void PrivateTwaddle(string reciverId, string message)  
    {  
        var reciver = Twaddlers.Find(x => Equals(x.ConnectionId, reciverId));  
        if (reciver == null)  
            return;  
      
        var sender = Twaddlers.Find(x => Equals(x.ConnectionId, Context.ConnectionId));  
        if (sender == null)  
            return;  
      
        var privateTwaddle = new TwaddleDetails()  
        {  
            Twaddler = sender.Name,  
            TwaddleContent = message  
        };  
      
        Clients.Client(reciverId).PrivateTwaddle(sender.ConnectionId, privateTwaddle);  
        Clients.Caller.PrivateTwaddle(reciver.ConnectionId, privateTwaddle);  
    }  


And lastly, inform other clients when this client gets disconnected.



    public override Task OnDisconnected()  
    {  
        var twaddler = Twaddlers.Find(x => Equals(x.ConnectionId, Context.ConnectionId));  
        if (twaddler != null)  
        {  
            Twaddlers.Remove(twaddler);  
      
            //broadcast the twaddler has loggedout to all twaddlers  
            Clients.All.BoradcastTwaddlerLogOut(twaddler);  
        }  
        return base.OnDisconnected();  
    }  


Finally, you have successfully used SignalR to build your own real-time application which allows users to create private as well as global chat rooms by just writing a few server and client methods. Click here to get to the Twaddler project repository.

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

 

 

 



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