SignalR - 找不到类型或命名空间名称“Hub”

时间:2013-02-06 10:27:24

标签: signalr signalr-hub

我是SignalR库的新手。我在VS2010中创建了一个新的Web项目,然后在Package Manager Console中执行了以下命令来设置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上也遇到了同样的问题。

3 个答案:

答案 0 :(得分:3)

Hub类已移至RC2中的其他命名空间。请转到Microsoft.AspNet.SignalR

答案 1 :(得分:1)

public class LetsChat : Hub更改为

public class LetsChat : Microsoft.AspNet.SignalR.Hub

答案 2 :(得分:0)

接受的答案对我不起作用。在我的.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);
        }
    }
}