简体   繁体   English

在SignalR Hub中引用另一个项目/ winform

[英]Give a reference to another project/winform in SignalR Hub

I am working on SignalR For Winforms official example. 我正在研究SignalR For Winforms的官方示例。 The way they handle UI interaction is using the Program instance/reference as you can see in the code below: 他们处理UI交互的方式是使用Program实例/引用,如下面的代码所示:

public class MyHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }
    public override Task OnConnected()
    {
        //here they used Program....
        Program.MainForm.WriteToConsole("Client connected: " + Context.ConnectionId);
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool graceFull)
    {
        Program.MainForm.WriteToConsole("Client disconnected: " + Context.ConnectionId);
        return base.OnDisconnected(graceFull);
    }
}

My question is, I have created an empty solution in Visual Studio and I added 2 projects, one is a WinForms project, The other is a class library. 我的问题是,我在Visual Studio中创建了一个空解决方案,我添加了2个项目,一个是WinForms项目,另一个是类库。

Obviously I can not give a Program reference from WinForms project in the MyHub class which is in the class library project since as far as I know the Hub or MyHub class should not be instantiated...its just there for the sake of Reflection. 显然我不能在MyHub项目中的MyHub类的WinForms项目中给出一个Program引用,因为据我所知, HubMyHub类不应该被实例化......它只是为了反射而存在。

Is there a way that I can pass a reference to my winforms to the MyHub class either statically or something? 有没有办法可以静态地将我的winforms引用传递给MyHub类?

Ok I just solved by adding a static field (an interface which is implemented by the Form : 好吧,我刚刚通过添加一个静态字段(一个由Form实现的接口)来解决:

public class MyHub : Hub
{
    public static ICommunicationOwner Owner;

    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }
    public override Task OnConnected()
    {
        Owner.GetData("Client connected: ", Context.ConnectionId);
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool graceFull)
    {
        Owner.GetData("Client disconnected: ", Context.ConnectionId);
        return base.OnDisconnected(graceFull);
    }
}

In the server class I did like this: 在服务器类中,我确实喜欢这样:

    public void StartServer()
    {
        MyHub.Owner = _owner;
        try
        {
            SignalR = WebApp.Start(_serverUri);
        }
        catch (TargetInvocationException e)
        {
            _isServerStarted = false;
            _owner.GetData("Connection", "Error starting server: " + e.Message);
            return;
        }
        catch (Exception e)
        {
            _isServerStarted = false;
            _owner.GetData("Connection", "Error starting server: " + e.Message);
            return;
        }

        _owner.GetData("Connection", "Successful");
        _isServerStarted = true;
    }

Alternatively my strongly recommendation is use SignalR Self-Host.This mechanism is very helpful for you. 或者我强烈建议使用SignalR Self-Host。这个机制对你很有帮助。 This is a step by step tutorial: 这是一步一步的教程:

Self-Host 自托管

This mechanism work like this: You are creating a signalr host which is like push data to specific port. 这种机制的工作方式如下:您正在创建一个信号器主机,就像将数据推送到特定端口一样。 This host can be console application,asp.net mvc application or winform etc. Then other project or class library will be client (Signalr has packages as 'SignalR Javascript Client' and 'SignalR .NET Client') . 这个主机可以是控制台应用程序,asp.net mvc应用程序或winform等。然后其他项目或类库将是客户端(Signalr包含'SignalR Javascript Client'和'SignalR .NET Client')。 Web application will take a message as a javascript client, other application such as winforms or console will take a message as .net client. Web应用程序将消息作为javascript客户端,其他应用程序(如winforms或控制台)将以.net客户端的形式接收消息。 This mechanism is a really scalable and extendable. 这种机制是一种真正可扩展和可扩展的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM