【发布时间】:2018-07-05 07:51:21
【问题描述】:
我有一个 ASP.NET Boilerplate v3.6.2 项目(.NET Core / Angular),我需要从后端方法调用客户端函数,所以我使用ASP.NET Core SignalR implementation。
我按照官方文档,所以:
在后台
-
在我的模块中,我将依赖项添加到
AbpAspNetCoreSignalRModule:[DependsOn(typeof(AbpAspNetCoreSignalRModule))] public class MyModule : AbpModule并将this NuGet 包添加到模块的项目中。
-
然后我扩展了
AbpCommonHub类以利用SignalR Hub 的内置实现,添加一个用于发送消息的SendMessage()方法:public interface IMySignalRHub : ITransientDependency { Task SendMessage(string message); } public class MySignalRHub: AbpCommonHub, IMySignalRHub { protected IHubContext<MySignalRHub> _context; protected IOnlineClientManager onlineClientManager; protected IClientInfoProvider clientInfoProvider; public MySignalRHub( IHubContext<MySignalRHub> context, IOnlineClientManager onlineClientManager, IClientInfoProvider clientInfoProvider) : base(onlineClientManager, clientInfoProvider) { AbpSession = NullAbpSession.Instance; _context = context; } public async Task SendMessage(string message) { await _context.Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message)); } } -
我将“/signalr”网址的映射更改为
MySignalRHub:public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { [...] # if FEATURE_SIGNALR app.UseAppBuilder(ConfigureOwinServices); # elif FEATURE_SIGNALR_ASPNETCORE app.UseSignalR(routes => { routes.MapHub<MySignalRHub>("/signalr"); }); # endif [...] } } -
然后我使用集线器在服务实现中发送消息:
public class MyAppService: AsyncCrudAppService<MyEntity, MyDto, int, PagedAndSortedResultRequestDto, MyCreateDto, MyDto>, IMyAppService { private readonly IMySignalRHub _hub; public MyAppService(IRepository<MyEntity> repository, IMySignalRHub hub) : base(repository) { _hub = hub; } public override Task<MyDto> Create(MyCreateDto input) { _hub.SendMessage("test string").Wait(); [...] } }
在客户端
所有配置和包含都已从原始模板中到位。当我打开 Angular 应用程序时,我可以看到这个控制台日志:
DEBUG: Connected to SignalR server!
DEBUG: Registered to the SignalR server!
当我尝试调用将消息发送到客户端的后端服务时,我在控制台中收到以下警告:
Warning: No client method with the name 'getMessage' found.
我尝试了在official documentation 和互联网上找到的许多解决方案,但都没有奏效。我无法在客户端代码中定义“getMessage”处理程序。
我尝试过的一些无效示例:
实施 1:
// This point is reached
abp.event.on('getMessage', userNotification => {
debugger; // Never reaches this point
});
实施 2:
// This point is reached
abp.event.on('abp.notifications.received', userNotification => {
debugger; // Never reaches this point
});
实施 3:
// This is taken from the official documentation and triggers the error:
// ERROR TypeError: abp.signalr.startConnection is not a function
abp.signalr.startConnection('/signalr', function (connection) {
connection.on('getMessage', function (message) {
console.log('received message: ' + message);
});
});
你有没有遇到过这种情况?你有 Angular 客户端中处理程序定义的简单工作示例吗?
更新
我尝试了这个替代解决方案,更改了SignalRAspNetCoreHelper 类(随基本模板一起提供的共享类)的实现:
export class SignalRAspNetCoreHelper {
static initSignalR(): void {
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
abp.signalr = {
autoConnect: true,
connect: undefined,
hubs: undefined,
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken),
remoteServiceBaseUrl: AppConsts.remoteServiceBaseUrl,
startConnection: undefined,
url: '/signalr'
};
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js', () => {
// ADDED THIS
abp.signalr.startConnection(abp.signalr.url, function (connection) {
connection.on('getMessage', function (message) { // Register for incoming messages
console.log('received message: ' + message);
});
});
});
}
}
现在在控制台中我可以看到两条消息:
Warning: No client method with the name 'getMessage' found.
SignalRAspNetCoreHelper.ts:22 received message: User 2: asd
所以它正在工作,但不完全。某处“getMessage”处理程序不可见。 使用 ASP.NET Boilerplate 在 Angular 中实现消息处理程序的正确方法是什么?
【问题讨论】:
标签: c# typescript asp.net-core aspnetboilerplate asp.net-core-signalr