Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SignalR service to .NET 8.0 #38

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions signalr-hubs/signalr-for-aspnet-core.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2037
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "signalr-for-aspnet-core", "signalr-for-aspnet-core\signalr-for-aspnet-core.csproj", "{C10DB25F-9FF8-4136-A05D-1869947045AF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "signalr-for-aspnet-core", "signalr-for-aspnet-core\signalr-for-aspnet-core.csproj", "{C10DB25F-9FF8-4136-A05D-1869947045AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
110 changes: 52 additions & 58 deletions signalr-hubs/signalr-for-aspnet-core/Hubs/ProductHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Task = System.Threading.Tasks.Task;

namespace signalr_for_aspnet_core.Hubs
namespace signalr_for_aspnet_core.Hubs;

public class ProductHub(SampleEntitiesDataContext context) : Hub
{
public class ProductHub : Hub
public override Task OnConnectedAsync()
{
public override System.Threading.Tasks.Task OnConnectedAsync()
{
Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnConnectedAsync();
}
Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnConnectedAsync();
}

public override System.Threading.Tasks.Task OnDisconnectedAsync(Exception e)
{
Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnDisconnectedAsync(e);
}
public override Task OnDisconnectedAsync(Exception e)
{
Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnDisconnectedAsync(e);
}

public IEnumerable<ProductSignalR> Read()
public IEnumerable<ProductSignalR> Read()
{
var products = context.Products.Select(p => new ProductSignalR
{
using (var context = new SampleEntitiesDataContext())
ID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = (double)p.UnitPrice.GetValueOrDefault(),
UnitsInStock = p.UnitsInStock.GetValueOrDefault(),
CreatedAt = DateTime.Now.AddMilliseconds(1),
Category = new CategorySignalR
{

var products = context.Products.Select(p => new ProductSignalR
{
ID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = (double)p.UnitPrice.GetValueOrDefault(),
UnitsInStock = p.UnitsInStock.GetValueOrDefault(),
CreatedAt = DateTime.Now.AddMilliseconds(1),
Category = new CategorySignalR()
{
CategoryID = p.Category.CategoryID,
CategoryName = p.Category.CategoryName
}
})
.ToList();

return products;
CategoryID = p.Category.CategoryID,
CategoryName = p.Category.CategoryName
}
}
}).ToList();

public ProductSignalR Create(ProductSignalR product)
{
product.ID = DateTime.Now.Ticks;
product.CreatedAt = DateTime.Now;
product.Category = product.Category ?? new CategorySignalR() { CategoryID = 1 };
return products;
}

Clients.OthersInGroup(GetGroupName()).SendAsync("create", product);
public ProductSignalR Create(ProductSignalR product)
{
product.ID = DateTime.Now.Ticks;
product.CreatedAt = DateTime.Now;
product.Category ??= new CategorySignalR { CategoryID = 1 };

return product;
}
Clients.OthersInGroup(GetGroupName()).SendAsync("create", product);

public void Update(ProductSignalR product)
{
Clients.OthersInGroup(GetGroupName()).SendAsync("update", product);
}
return product;
}

public void Destroy(ProductSignalR product)
{
Clients.OthersInGroup(GetGroupName()).SendAsync("destroy", product);
}
public void Update(ProductSignalR product)
{
Clients.OthersInGroup(GetGroupName()).SendAsync("update", product);
}

public string GetGroupName()
{
return GetRemoteIpAddress();
}
public void Destroy(ProductSignalR product)
{
Clients.OthersInGroup(GetGroupName()).SendAsync("destroy", product);
}

public string GetRemoteIpAddress()
{
return Context.GetHttpContext()?.Connection.RemoteIpAddress.ToString();
}
public string GetGroupName()
{
return GetRemoteIpAddress();
}

public string GetRemoteIpAddress()
{
return Context.GetHttpContext()?.Connection.RemoteIpAddress?.ToString();
}
}
}
14 changes: 4 additions & 10 deletions signalr-hubs/signalr-for-aspnet-core/Models/CategorySignalR.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace signalr_for_aspnet_core.Models;

namespace signalr_for_aspnet_core.Models
public class CategorySignalR
{
public class CategorySignalR
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
22 changes: 9 additions & 13 deletions signalr-hubs/signalr-for-aspnet-core/Models/ProductSignalR.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace signalr_for_aspnet_core.Models
namespace signalr_for_aspnet_core.Models;

public class ProductSignalR
{
public class ProductSignalR
{
public long? ID { get; set; }
public string ProductName { get; set; }
public double UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public DateTime? CreatedAt { get; set; }
public CategorySignalR Category { get; set; }
}
public long? ID { get; set; }
public string ProductName { get; set; }
public double UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public DateTime? CreatedAt { get; set; }
public CategorySignalR Category { get; set; }
}
Loading