Now we need to introduce two new middle-wares to Startup.cs. Open up your Startup.cs file and locate ConfigureServices method. Add below code segment before services.AddMvc();

services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>{

builder

.AllowAnyMethod()

.AllowAnyHeader()

.WithOrigins("http://localhost:4200");

})); services.AddSignalR();

Here we are adding Cross-origin policy to accept request from localshot:4200 and then we tell to use SignalR.

Now locate Configure method in the same Startup.cs class and add below code before app.UserMvc();

app.UseCors("CorsPolicy");

app.UseSignalR(routes =>

{

routes.MapHub<NotifyHub>("notify");

});

Here we are adding SignalR route to the api. In this way we can connect the clients by adding <hostedurl>:<port>/notify (eg:http://localhost:1874/notify) in the client application. I’ll show you how to do this in a while.

Now your final Startup.cs should look like this:

Startup.cs

Build your project and make sure everything works fine till this step :) .

Now let’s create simple message model. This is the object we are going to send via api. Create a new class named Message.cs and add below two properties.

public string Type { get; set; }

public string Payload { get; set; }

I simply created two properties to carry message type and payload. You can come up with your own model for this.

Now let’s create our controller. This will be our entry point to outside of the world.

Right click on the project and select add new controller. Then select empty web api and name it as MessageController.