我有一项服务,并希望增加通过 SignalR(在 OWIN 上)与之交互的能力。有很多关于如何向客户端发送消息的示例,但是如何从客户端接收消息并将它们转发到父服务?
例如
public class MyService
{
...
public void LaunchSR()
{
_srWebApp = WebApplication.Start<SrStartup>("http://localhost:" + _signalRPortNumber.ToString());
_srContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
}
public class SrStartup
{
public void Configuration(Owin.IAppBuilder app)
{
app.MapConnection("/raw", typeof(PersistentConnection), new ConnectionConfiguration { EnableCrossDomain = true });
app.MapHubs();
}
}
public class MyHub : Hub
{
public void SendToServer(string data)
{
//!! Don't have a reference to MyService instance,
//!! so LocalCommand is out of scope
LocalCommand(data, null);
}
}
public void LocalCommand(data)
{
// Do Stuff in the main service context, accessing private objects etc.
}
}
里面的代码SendToServer()
有编译时错误:
“无法通过嵌套类型 MyHub 访问外部类型 MyService 的非静态成员”。
我理解为什么会发生这种情况,但不知道如何正确执行此操作。