Skip to content

Instantly share code, notes, and snippets.

View nishanc's full-sized avatar
🚀
This is the way!

Nishan Chathuranga Wickramarathna nishanc

🚀
This is the way!
View GitHub Profile
IDerived value = new DerivedImplement { Base = 0, Derived = 1 };
JsonSerializer.Serialize(value); // {"Base":0,"Derived":1}
public interface IBase
{
public int Base { get; set; }
}
public interface IDerived : IBase
{
[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)]
public class MyPoco
{
public int Id { get; set; }
}
JsonSerializer.Deserialize<MyPoco>("""{"Id" : 42, "AnotherId" : -1 }""");
// JsonException : The JSON property 'AnotherId' could not be mapped to any .NET member contained in type 'MyPoco'.
// Set JsonSerializerOptions.UnmappedMemberHandling to either Skip or Disallow.
this.http.post<Token>(`${this.apiUrl}/Auth/login`, body, httpOptions).subscribe({
next: (response) => {
const tokenInfo = this.getDecodedAccessToken(response.tokenString); // decode token
const user = tokenInfo.unique_name;
const role = tokenInfo.role;
this.signalRService.joinGroupFeed(role); // join the group
this.localStorageService.setItem('user', user);
this.notificationService.success("Logged in");
[HttpPost]
public async Task<ActionResult> PostTodo(TodoDto todo)
{
var newTodo = new Todo
{
Name = todo.Name
};
await _repo.AddTodo(newTodo);
// await _notificationHub.Clients.All.SendNotificationAsync("Added Item Notification from Server");
await _notificationHub.Clients.Group(UserGroups.Administrator)
using Microsoft.AspNetCore.SignalR;
namespace ServerApp.SignalR;
public class NotificationHub : Hub<INotificationHub>
{
public async Task SendNotificationToUserAsync(string message)
{
await Clients.All.SendNotificationAsync(message);
}
constructor(private http: HttpClient,
private notificationService: NotificationService,
private signalRService: SignalRService,
private eventHandlerService: EventHandlerService) {
this.eventHandlerService.notificationEvent.subscribe((data: string) => {
this.onNotificationEvent(data);
});
}
onNotificationEvent(data: string) {
import { EventEmitter, Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class EventHandlerService {
notificationEvent: EventEmitter<any> = new EventEmitter<any>();
}
import { Injectable } from '@angular/core';
import * as signalR from '@microsoft/signalr';
@Injectable({
providedIn: 'root'
})
export class SignalRService {
private hubConnection: any;
public startConnection(){
using Microsoft.EntityFrameworkCore;
using ServerApp.Data;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using ServerApp.Repositories;
using ServerApp.SignalR;
var builder = WebApplication.CreateBuilder(args);
var corsPolicy = "AllowAngularOrigins";
using Microsoft.AspNetCore.SignalR;
namespace ServerApp.SignalR;
public class NotificationHub : Hub<INotificationHub>
{
public async Task SendNotificationToUserAsync(string message)
{
await Clients.All.SendNotificationAsync(message);
}