我有一個class
,我想要Cloudscribe SiteId
,但我不確定如何訪問它。在Controller或Views外部訪問Cloudscribe SiteID
如果我使用的是controller
,那麼我將能夠依賴SiteContext
將DI'd轉換爲構造函數,但在這種情況下不起作用。
有人能告訴我如何在下面的代碼中訪問SiteId
屬性?
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoLabR
{
public class Messaging : Hub
{
private string _siteId;
public Messaging()
{
//Code here to get SiteID
_siteId = "<<Code for getting Site ID";
}
public Task Post(string message)
{
return Clients.Group(_siteId).InvokeAsync("Post", message);
}
public Task JoinRoom()
{
Groups.AddAsync(Context.ConnectionId, _siteId);
return Clients.Group(_siteId).InvokeAsync("Post", Context.User.Identity.Name + " joined. " + _siteId);
}
}
}
謝謝@Pawel我錯誤地認爲,因爲SiteContext可能已經注入到網站上的控制器中,它也適用於我的集線器,但看起來我需要在我的'startup.cs'中明確地引用它。 –