我试图使用SignalR.EventAggregatorProxy 库,但我无法让我的头围绕文档或演示。我不需要使用Ninject,我只需要在服务器端和Angular SPA上使用WEB API的简单测试应用程序。SignalR.EventAggregatorProxy简单示例问题
这是我到目前为止有:
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
var proxy = new Lazy<IEventAggregator>(() => new Eventer());
GlobalHost.DependencyResolver.Register(typeof(IEventAggregator),() => proxy.Value);
app.MapEventProxy<Message>();
}
}
消息类
public abstract class Message {}
public class MyMessage : Message
{
public int Number { get; set; }
}
Eventer.cs类射击事件
public class Eventer : IEventAggregator
{
public void Subscribe(Action<object> handler)
{
Task.Factory.StartNew(() =>
{
int number = 0;
while (true)
{
handler.Invoke(new MyMessage { Number = number++ });
System.Threading.Thread.Sleep(1000);
}
});
}
}
简单的应用程序.js
(function() {
'use strict';
var app = angular.module("TestApp", ['signalR.eventAggregator']);
app.controller("TestCtrl", ['$scope','$http', function ($scope, $http) {
function onEvent(e) {
console.log("event received => ", e);
};
$scope.eventAggregator().subscribe(EventAgrTest.Events.MyMessage, onEvent);
$scope.headerText = "HEADER";
}]);
})();
和index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="TestApp">
<head>
<title></title>
<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.js"></script>
<script src="Scripts/jquery.signalR.eventAggregator-1.4.141.0.js"></script>
<script src="/signalr/hubs"></script>
<script src="/eventAggregation/events"></script>
</head>
<body ng-controller="TestCtrl">
<h1>{{headerText}}</h1>
<script src="Scripts/angular.js"></script>
<script src="Scripts/jquery.signalR.eventAggregator.angular-1.4.143.0.js"></script>
<script src="Scripts/app.js"></script>
</body>
</html>
当我运行应用程序我得到:指向错误 “类型错误无法读取属性未定义的 '订阅'” 排队
signalR.eventAggregator.subscribe(type, function(e) { ...
在jquery.signalR.eventAggregator.angular-1.4.143.0.js
我敢肯定,我失去了一些东西,但我不知道是什么。
Wiki中有一部分叫做Implement约束处理程序(https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki/Implement-constraint-handlers),它应该像从抽象类继承一样简单,但我不确定需要从这个类继承什么。
啊,真的错过了。将在文档中更清楚地说明。我可能会在将来更改,以便客户端脚本(jquery.signalR.eventAggregator-1.4.141.0.js)不直接使用事件聚合器。那么这个命令并不重要。这将是一个突破性的变化,因为那么库的用户需要使用事件聚合器 – Anders
我确保这不会再发生https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/commit/f438fe7994b1151d296bd6534cde1657f81cc2a8 – Anders