This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.SignalR; | |
namespace ServerApp.SignalR; | |
public class NotificationHub : Hub<INotificationHub> | |
{ | |
public async Task SendNotificationToUserAsync(string message) | |
{ | |
await Clients.All.SendNotificationAsync(message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter, Injectable } from '@angular/core'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class EventHandlerService { | |
notificationEvent: EventEmitter<any> = new EventEmitter<any>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import * as signalR from '@microsoft/signalr'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class SignalRService { | |
private hubConnection: any; | |
public startConnection(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.SignalR; | |
namespace ServerApp.SignalR; | |
public class NotificationHub : Hub<INotificationHub> | |
{ | |
public async Task SendNotificationToUserAsync(string message) | |
{ | |
await Clients.All.SendNotificationAsync(message); | |
} |