让我们有一个强类型的集线器:
public class TestHub : Hub<IClient>
{
public void DoSomething()
{
Clients.Caller.UserPriority = 1;
}
}
IClient如下:
public interface IClient
{
int UserPriority { get; set; }
}
当我试图打电话给DoSomething时,我收到以下错误:
Unhandled Exception: System.InvalidOperationException: The interface 'IClient' m
ust not contain any properties.
at Microsoft.AspNetCore.SignalR.Hubs.TypedClientBuilder`1.VerifyInterface(Typ
e interfaceType)
at Microsoft.AspNetCore.SignalR.Hubs.TypedClientBuilder`1.GenerateClientBuild
er()
at Microsoft.AspNetCore.SignalR.Hubs.TypedClientBuilder`1.<>c.<.cctor>b__12_0
()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at Microsoft.AspNetCore.SignalR.Hubs.TypedClientBuilder`1.Build(IClientProxy
proxy)
at CallSite.Target(Closure , CallSite , Type , Object )
at Microsoft.AspNetCore.SignalR.Hubs.TypedHubCallerConnectionContext`1.get_Ca
ller()
但是当我创建一个非强类型的集线器时,我可以毫无错误地执行以下操作(使用动态属性):
public class TestHub : Hub
{
public void DoSomething()
{
Clients.Caller.UserPriority = 1;
}
}
我不明白这种行为。为什么我不能在强类型Hub中为客户端设置属性?
感谢您的回答。