2

我是 SignalR 库的新手。我在 VS2010 中创建了一个新的 Web 项目,然后在包管理器控制台中执行以下命令来设置 SignalR 包。

PM> Install-Package Microsoft.Aspnet.SignalR -pre

So it downloaded all the necessary files into the application.
----------
Successfully installed 'Microsoft.AspNet.SignalR 1.0.0-rc2'.
Successfully added 'jQuery 1.6.4' to demo.
Successfully added 'Microsoft.AspNet.SignalR.JS 1.0.0-rc2' to demo.
Successfully added 'Newtonsoft.Json 4.5.11' to demo.
Successfully added 'Microsoft.AspNet.SignalR.Core 1.0.0-rc2' to demo.
Successfully added 'Owin 1.0' to demo.
Successfully added 'Microsoft.AspNet.SignalR.Owin 1.0.0-rc2' to demo.
Successfully added 'Microsoft.Web.Infrastructure 1.0.0.0' to demo.
Successfully added 'Microsoft.Owin.Host.SystemWeb 1.0.0-rc2' to demo.
Successfully added 'Microsoft.AspNet.SignalR.SystemWeb 1.0.0-rc2' to demo.
Successfully added 'Microsoft.AspNet.SignalR 1.0.0-rc2' to demo.
----------

现在我添加了一个名为LetsHub.cs.

在该文件中,我编写了以下代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;

namespace demo.Hubs
{
  [HubName("chatHub")]
  public class LetsChat : Hub
  {
      public void SendMsg(string Message)
      {
         Clients.All.addMessage(Message);
      }
  }
}

我无法继承这个Hub类。

Type or Namespace name Hub cound not be found ( are you missing using directive or assemblr reference ? ).

我在 VS 2012 上也面临同样的问题。

4

3 回答 3

3

Hub 类已移至 RC2 中的不同命名空间。请更改为 Microsoft.AspNet.SignalR

于 2013-02-06T22:25:01.127 回答
1

更改public class LetsChat : Hub

public class LetsChat : Microsoft.AspNet.SignalR.Hub
于 2014-02-20T03:04:54.380 回答
1

接受的答案对我不起作用。在我的 .NET Core 3.1 中,这是我的代码:

using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace SignalRChat.Hubs
{
    public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message).ConfigureAwait(false);
        }
    }
}
于 2020-04-14T03:26:24.697 回答