简体   繁体   English

N层WCF应用程序中的SignalR

[英]SignalR in an N-Tier WCF application

I have an n-tier application, which has a WCF service exposing all my business logic, hosted as a windows service, with a MVC application as the client, consuming the services. 我有一个n层应用程序,该应用程序具有一个WCF服务,该服务公开了我所有的业务逻辑,并作为Windows服务托管,而MVC应用程序作为客户端使用了这些服务。

Most of the examples out there shows SignalR within the MVC application. 那里的大多数示例都显示了MVC应用程序中的SignalR。 I have tried extracting the Hub out into a separate DLL like this: 我尝试将集线器提取到一个单独的DLL中,如下所示:

Hub.dll Hub.dll

public class Chat : Hub
{

    public void Send(string message)
    {
        Clients.All.addMessage(message);
    }
}

and trying to call the Send() from my MVC cshtml, even after adding Hub.dll as a reference, doesn't work. 并尝试从我的MVC cshtml调用Send(),即使将Hub.dll添加为引用后也无法正常工作。

This is the javascript in my cshtml file: 这是我的cshtml文件中的javascript:

<script type="text/javascript">
    $(function() {
        // Proxy created on the fly          
        var chat = $.connection.chat;

        // Declare a function on the chat hub so the server can invoke it          
        chat.client.addMessage = function(message) {
            $('#messages').append('<li>' + message + '</li>');
        };

        // Start the connection
        $.connection.hub.start().done(function() {
            $("#broadcast").click(function() {
                // Call the chat method on the server
                chat.server.send($('#msg').val());
            });
        });
    });
</script>

While the code above is a very basic example, an example of what I would like to accomplish would be similar to those SignalR progress bar tutorials out there, except the progress reporting will be done by my business layer. 尽管上面的代码是一个非常基本的示例,但是我要完成的示例与那里的那些SignalR进度条教程类似,只是进度报告将由我的业务层完成。

To further elaborate, I would like to do something like this: 为了进一步阐述,我想做这样的事情:

1) MVC client calls PerformLongRunningTaskA() through the hosted WCF service. 1)MVC客户端通过托管的WCF服务调用PerformLongRunningTaskA()。

2) WCF service invokes method in business layer 2)WCF服务在业务层中调用方法

3) Business layer starts PerformLongRunningTaskA() 3)业务层启动PerformLongRunningTaskA()

4) Business layer reports progress back to MVC client 10%..20%..etc till 100% (using SignalR?) 4)业务层向MVC客户端报告进度10%.. 20%.. etc直到100%(使用SignalR?)

This how my project structure is roughly like: presentation - MVC app service layer - WCF service (hosted on windows service) business layer - all my business logic data layer - Entityframework 我的项目结构大致如下:演示文稿-MVC应用程序服务层-WCF服务(托管在Windows服务上)业务层-我所有的业务逻辑数据层-Entityframework

EDIT: 编辑:

The above now works. 以上工作。 It was some javascript error on my part. 我这是一些JavaScript错误。

I have created another console app, to simulate my business DLL to trigger off signalr to broadcast an event like this: 我创建了另一个控制台应用程序,以模拟我的业务DLL以触发Signalr广播事件,如下所示:

class Program
{
    static void Main(string[] args)
    {
      Say("HEY");
    }
    public static void Say(string message)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<Chat>();
        context.Clients.All.say(message);
    }
}

I have added Hub.dll as a reference to my console project, but the above now doesn't work. 我已添加Hub.dll作为对我的控制台项目的引用,但是以上内容现在不起作用。 No error messages, nothing. 没有错误信息,什么都没有。 it just runs normally, but my MVC app doesn't display the message. 它只能正常运行,但是我的MVC应用程序不显示该消息。

If you pretend to broadcast messages you need to be in the "Service context", what I mean by that is that you have to have access to the hosted Hub context. 如果您假装广播消息,则需要位于“服务上下文”中,这意味着您必须有权访问托管的Hub上下文。

If you need to broadcast from a Console app, you can create methods in your Hub that allow you to do so, and make your console application a client of the main SignalR server. 如果需要从控制台应用程序广播,则可以在集线器中创建允许您进行广播的方法,并使控制台应用程序成为SignalR主服务器的客户端。 Details on how to create a .net console SignalR client: SignalR Client Hubs 有关如何创建.net控制台SignalR客户端的详细信息: SignalR客户端中心

If you need to have the "hosted context" in the console app, then try SelfHost SignalR SelfHost 如果您需要控制台应用程序中的“托管上下文”,请尝试使用SelfHost SignalR SelfHost

Cheers, 干杯,

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

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