WCF服务与SignalR [英] WCF Service with SignalR

查看:568
本文介绍了WCF服务与SignalR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这对仪表盘数图表的Web应用程序。对图表中的数据是在对客户端的doc​​ument.ready函数调用WCF服务方法获取。

I have a web application which has few charts on dashboard. The data for charts is fetched on document.ready function at client side invoking a WCF service method.

现在我想是在我的应用程序中使用SignalR。我是真正的新SignalR。我怎样才能调用WCF方法从SignalR集线器或者你可以说的是,而不是从服务器上获取数据,我想WCF服务将数据推到客户机每隔一分钟。

What i want is now to use SignalR in my application. I am really new to SignalR. How can i call WCF methods from SignalR Hub or what you can say is that instead of pulling data from server i want the WCF service to push data to client every one minute.

有signalR和WCF服务之间的通信的一种方式。

Is there a way of communication between signalR and WCF service.

另一种方法可以强制客户端每分钟索要数据WCF服务。

Also another approach can be to force client to ask for data from WCF Service every minute.

任何帮助将是非常美联社preciated。

Any help will be really appreciated.

我已经做了以下的呢。

我的仪表盘页面上的客户端功能

Client Side Function on my Dashboard page

<script src="Scripts/jquery.signalR-2.0.3.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="/signalr/hubs"></script>
<a id="refresh">Refresh</a>
    $(function() {
            var dashboardHubProxy = $.connection.dashboardHub;
            $.connection.hub.start().done(function() {
                // dashboardHubProxy.server.refreshClient(parameters);
                $("#refresh").click(function() {
                    dashboardHubProxy.server.refreshClient(parameters);
                });
            });
            dashboardHubProxy.client.refreshChart = function (chartData) {
                debugger;
                DrawChart(chartData, 'Hourly Call Count For Last ' + Duration + ' Days', '#chartHourly', 'StackedAreaChart');
            };
        });

和我的仪表盘毂类如下:

and my Dashboard Hub class is as follows

public class DashboardHub : Hub
{
    private readonly ReportService ReportService = new ReportService();


    public void RefreshClient(string parameters)
    {
        var chartData = ReportService.GenerateHourlyCallsTrendGraphicalReport(parameters);
        Clients.All.refreshChart(chartData);
    }
}

我SignalR启动类如下:

My SignalR startup class is as follows

[assembly: OwinStartup(typeof(CallsPortalWeb.Startup), "Configuration")]
namespace CallsPortalWeb
{
    public static class Startup
    {
        public static void Configuration(IAppBuilder app)
        {
            ConfigureSignalR(app);
        }
        public static void ConfigureSignalR(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

当我在刷新按钮和关于枢纽RefreshClient方法的调试器点击调试器不会去,这意味着我无法拨打SignalR的服务器端方法的方法。

When i click on refresh button and a debugger on RefreshClient method on hub the debugger doesn't get to the method which means i am unable to call server side method of SignalR.

有什么需要在web.config中做?

Is there anything needs to be done in web.config?

推荐答案

我同意AD.Net的评论。要精心虽然略多,SignalR集线器可以直接在Web项目还挺均采用同样的方法控制器主持。还有一个包在那里,所以你可以承载它自己的SignalR库,以便它可以为所有的本身就是一个服务的行为。无论哪种方式,你需要先打SignalR枢纽,因为这是它如何沟通,然后你会打电话给你的WCF服务方法从集线器内。

I agree with AD.Net's comment. To elaborate slightly more though, the SignalR hubs can be hosted directly in your web project kinda the same way controllers are used. There is also a package out there so you can host the SignalR library on its own so it can act as a service all on its own. Either way you will need to hit the SignalR hub first as that is how it communicates then you would call your WCF service methods from within the hubs.

简要说明

您HUB将通过您的用户客户端,你的WCF客户端都使用方法。您可以使用类似 UserConnected()供用户调用和设置您的连接日志记录。然后,WCF服务可能会打电话给你的HUB与 UpdateUserStats(GUID connnectionId,UserStats统计)这又将直接调用用户客户端,并提供通过在像这样的统计 Clients.Client(的ConnectionId).updateStats(统计)这反过来将有一个名为用户客户端 updateStats()上的方法这将处理接收到的信息。

Your HUB will have methods used by both your USER Client and your WCF Client. You may use something like UserConnected() for the user to call in and setup your logging of the connection. Then the WCF service may call your HUB with an UpdateUserStats(Guid connnectionId, UserStats stats) which would in turn call the USER client directly and provide the stats passed in like so Clients.Client(connectionId).updateStats(stats) which in turn would have a method on the USERS client named updateStats() that would handle the received information.

初始页面登陆

什么AD.Net提供的是基础code将被调用时,页面上的用户的土地。在这一点上,你会想记录,因此您可以直接联系他们回与该用户的ConnectionId。

What AD.Net provided is basic code that will be called when the user lands on the page. At this point you would want to log the ConnectionId related to that user so you can directly contact them back.

与轮毂接触WCF的首次接触

从集线器,你可以调用WCF服务,你通常会任何正常的C#code里面获取数据或执行操作,并返回给用户。

From your Hub, you could call your WCF service as you normally would inside any normal C# code to fetch your data or perform action and return it to your user.

更新用户的方式定期

SignalR删除您的客户端code,需要有不断轮询更新服务器。它的目的是让你与他们进行直接询问其推数据到客户端。这就是连接的持续发挥作用。

SignalR removes the need for your client code to have to continually poll the server for updates. It is meant to allow you to push data out to the client with out them asking for it directly. This is where the persistence of the connections come into play.

您可能希望创建一个包装器轻松地从你的应用程序将消息发送到集线器,因为你正在使用WCF我会假设你有这层背后的商业逻辑,所以你会想WCF服务深入到你的集线器每当动作X发生。为此,您可以通过利用客户端的C#code在这种情况下,你的客户实际上是在用户和WCF服务。的随着聊天应用程序其他用户,基本上是做你想让你的WCF服务做什么,这是发送消息给其他客户端。

You will probably want to create a wrapper to easily send messages to the hub from your application, since you are using WCF I would assume you have your business logic behind this layer so you will want the WCF service reaching out to your Hub whenever action X happens. You can do that by utilizing the Client side C# code as in this case your client is actually the user and the WCF service. With a chat application the other user is basically doing what you want your WCF service to do, which is send a message to the other client.

使用示例

您正在运行一个在线商店。仪表板显示多少订单已经出现了一天。所以,你会连线到集线器的通话将消息发送到更新订购的产品,当用户放置一个新的订单。您可以通过发送至您所配置的管理组做到这一点,在仪表盘上的任何管理员会得到的消息。但如果这些统计数据是非常特定用户,你就会更可能再达到,而不是到数据库中,找到的ConnectionId用户已相连接,并直接发送更新消息到的ConnectionId。

You are running an online store. The dashboard displays how many orders there have been for the day. So you would wire up a call to the hub to send a message out to update the products ordered when a user places a new order. You can do this by sending it to the admin group you have configured and any admins on the dashboard would get the message. Though if these stats are very user specific, you will more then likely instead reach into the database, find the ConnectionId that the user has connected with and send the update message directly to that connectionid.

WCF客户端code示例

只是柜面你需要一些code,这是直接从MS网站上与.NET客户端连接。你会在你的WCF服务使用,或任何在code你打算连接,然后发送更新到你的用户。

Just incase you want some code, this is directly from MS site on connecting with a .net client. You would use this in your WCF service, or wherever in your code you plan on connecting and then sending an update to your user.

var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));
await hubConnection.Start();

下面是直接链接到.NET客户端部分:<一href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client\">http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client

Here is a link directly to the .Net Client section: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client

我相信你已经看到了这个链接,但它确实拥有所有你需要的信息,好上手。 http://www.asp.net/signalr

I am sure you have seen this link but it really holds all the good information you need to get started. http://www.asp.net/signalr

下面是进入与code你用法更直接的联系。 <一href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server\">http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server

Here is a more direct link that goes into usages with code for you. http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server

增加:下面是具体的与SignalR和他们的投票仪表板一个博客。
<一href=\"http://solomon-t.blogspot.com/2012/12/signalr-and-interval-polling-for.html\">http://solomon-t.blogspot.com/2012/12/signalr-and-interval-polling-for.html

ADDED: Here is a blog specific to Dashboards with SignalR and their polling. http://solomon-t.blogspot.com/2012/12/signalr-and-interval-polling-for.html

新增:这里是管理用户signalR连接的页面。
<一href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections\">http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections

ADDED: Here is a page on managing users signalR connections. http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections

更新您的code更新

(中的NuGet).NET客户端库让您的.NET code接入集线器。既然你是一个客户,您将需要连接到集线器一样,谁也是一个客户端的用户。集线器将作为服务器这一点。因此,与.NET客户端我假设你会设置一个窗口服务,将内部查询,或基于一些事件会调用它的.NET客户端code部分,其将接触到你的集线器。集线器将采取提供的信息,很可能一个或的ConnectionId groupId和广阔的投更多的用户(这也许是一个网站上,因此将是JS客户端),将更新用户的客户端前端的方法。基本上我提起简要说明下。

The .Net Client library (in NuGet) gives your .net code access to the hub. Since you are a client you will need to connect to the hub just like the User who is also a client. Your hub would act as the server for this. So with the .Net Client I am assuming you would setup a windows service that would internally poll, or something event based that would call the .Net Client code portion of it which would reach out to your hub. Your hub would take the information provided, more than likely a ConnectionId or GroupId and broad cast the User (which is perhaps on a website so it would be the JS client) a method that would update the front end for the user client. Basically what I mention under "Brief Explanation".

现在,可以直接到code你贴回应。也就是说Javascript中,我所期望的连接像你这样做。更新初始连接上的图表是罚款为好。如果这是所有code signalR虽然明智的你缺少一个客户端的方法来处理刷新。技术上,而不是调用Clients.Caller.RefreshChart()你可以只返回数据,并使用它,这是你的JavaScript是现在在做什么。您在返回void但它期待你的约会。

Now, to directly respond to the code you posted. That is Javascript, I would expect a connect like you have done. Updating the chart on initial connection is fine as well. If this is all the code signalR wise though you are missing a client side method to handle the refresh. Technically, instead of calling Clients.Caller.RefreshChart() you could just return that data and use it, which is what your javascript is doing right now. You are returning void but it is expecting a your date.

现在,我实际上说的正确您的JavaScript,而不是纠正枢纽code。为什么?因为有您的客户端上JS的方法被称为refreshChart()可重复使用,当你有你的服务器伸手更新客户端。

Now, I would actually say correct your javascript instead of correcting the hub code. Why? Because having a method in JS on your client that is called "refreshChart()" can be reused for when you are having your server reach out and update the client.

因此​​,我建议,放弃任何涉及到更新你的JS做了发言仪表板。如果你想要做一个通知或东西的是好的用户,但不更新电网。

So I would recommend, dropping anything that is related to updating the dashboard in your JS done statement. If you want to do a notification or something to the user that is fine but dont update the grid.

现在创建一个名为refreshChart一个JS客户端功能,请注意小写R,您可以在C#中的大ř但JS库调用它会小写它所以当你做的函数就会收到您的仪表盘的信息

Now create a JS client function called "refreshChart", note the lower case R, you can call it with a big R in c# but the js library will lowercase it so when you make the function have it will receive your dashboard information.

现在,在服务器轮询,或者在一些操作执行,你的WCF会呼吁集线器会说的方法UpdateDashboar(ConnectionId之外dashInfo),然后该方法将其内部称之为refreshChart就像你在RefreshClient方法都在做,接受而不是做Clients.Caller你会使用Clients.Client(的ConnectionId).refreshChart(chartInfo)。

Now, on the server polling, or executing on some action, your WCF would call a method on the hub that would be say "UpdateDashboar(connectionId,dashInfo)" and that method would then inside of it call the "refreshChart" just like you are doing in your RefreshClient method, accept instead of doing Clients.Caller you would use Clients.Client(connectionId).refreshChart(chartInfo).

直接在code不工作的原因是因为你需要把这个空白到你期望得到返回的类型。如果剩下的就是codeD右你将有更新一次。你需要,如果你希望它不断更新来实现我所提到的其他逻辑。这又是为什么我问你是如何坚持您的连接。我添加了一个链接来帮助你,如果你不知道我在说什么。

Directly the reason your code is not working is because you need to turn that Void into the type you expect to be returned. If the rest is coded right you will have it update once. You will need to implement the other logic I mentioned if you want it constantly updating. Which is again why I asked about how you are persisting your connections. I added a link to help you with that if you are not sure what I am talking about.

这篇关于WCF服务与SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆