英文:
Hub signalr not found in Maui library class
问题
I've just created a new MAUI library project
and added a package from NuGet, Microsoft.AspNetCore.SignalR.Client
. Nevertheless, at the class
level, the Hub
cannot be found, and I am getting the error:
The type or namespace Hub could not be found.
It's strange because in another project like Blazor App
, there is no issue at all when doing the same. Any thoughts?
Full code:
using System;
using Microsoft.AspNetCore.SignalR;
namespace ServicesMauiLib
{
public class ChatHub : Hub
{
public Task SendMessage(string user, string message)
{
return Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
英文:
I've just created new MAUI library project
and added package from nuget Microsoft.AspNetCore.SignalR.Client
nevertheless on class
level Hub
is not found and i am getting error:
The type or namespace Hub could not be found.
Strange as in other project like Blazor App
there is no issue at all doing the same. Any thoughts?
Full code:
using System;
using Microsoft.AspNetCore.SignalR;
namespace ServicesMauiLib
{
public class ChatHub : Hub
{
public Task SendMessage(string user, string message)
{
return Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
答案1
得分: 2
SignalR的Hub
是用于服务器应用程序的内容,因此将其包含在Microsoft.AspNetCore.SignalR.Client
包中没有意义。
Hub
类曾包含在核心的Microsoft.AspNetCore.SignalR
中,但现在已被弃用。
> 在3.1中,我们转向了“共享框架”的概念。这样做可以使我们不必发布之前发布的许多包,它们现在在构建ASP.NET Core应用程序时默认包含在内。这就是发生在SignalR服务器包上的情况,它们现在包含在共享框架中,我们不再发布独立包。
>
> 另一个常见的问题是“如何编写一个用于服务器应用程序的使用SignalR的库?”。这可以通过在库中添加对共享框架的引用来完成。有关详细信息,请参阅https://docs.microsoft.com/aspnet/core/fundamentals/target-aspnetcore?view=aspnetcore-6.0&tabs=visual-studio。
因此,基本上需要将以下内容添加到库的.csproj
文件中。
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
英文:
SignalR Hub
is something that you use on a server application so it doesn't make sense to be included in Microsoft.AspNetCore.SignalR.Client
package.
The Hub
class was included in the core Microsoft.AspNetCore.SignalR
which is now deprecated.
> In 3.1 we moved to the "shared framework" concept. This made it so we
> don't need to release a lot of the packages that were released before
> and they are now included by default when you build an ASP.NET Core
> application. This is what happened to the SignalR server packages,
> they are now included in the shared framework and we no longer produce
> a standalone package.
>
> Another common question is "how do I write a library for server
> applications that uses SignalR?". This can be done by adding a
> reference to the shared framework in your library. See
> https://docs.microsoft.com/aspnet/core/fundamentals/target-aspnetcore?view=aspnetcore-6.0&tabs=visual-studio
> for details.
So basically you need to add the following to the .csproj
file of your library.
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论