【问题标题】:"IApplicationBuilder" does not contain a definition for "MapSignalR"“IApplicationBuilder”不包含“MapSignalR”的定义
【发布时间】:2017-07-31 19:55:20
【问题描述】:

我正在尝试在 ASP.NET Core 项目的 Startup 类上注册 SignalR。当我尝试调用 app.MapSignalR() 在项目上注册和启用 SignalR 时,它给了我:

“IApplicationBuilder”不包含“MapSignalR”的定义...

知道为什么吗?如何正确注册signalR?其他人处理同样的问题?

尝试安装 Microsoft.AspNet.WebApi.OwinSelfHostMicrosoft.Owin 但没有成功;(

  • Microsoft.AspNet.SignalR 2.2.1 及其依赖项均已安装
  • 目标框架设置为 .NET 4.5.2
  • 代码是 VS2017 自动生成的

跟随我的 Startup.cs 类文件(它是由 VS 自动生成的)

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WebApplication1.Data;
using WebApplication1.Models;
using WebApplication1.Services;

namespace WebApplication1
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
            {
                // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
                builder.AddUserSecrets<Startup>();
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseIdentity();

            app.MapSignalR(); // "IApplicationBuilder" does not contain a definition for "MapSignalR"...

            // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

【问题讨论】:

    标签: signalr asp.net-core-mvc visual-studio-2017 asp.net-core-middleware


    【解决方案1】:

    您需要添加另一个using 语句。基于这个示例:https://github.com/aspnet/SignalR-Client-Cpp/blob/c152e53fc27d4a7670baa3a7953b99edeab88be5/samples/SignalRServer/Startup.cs,试试这个:

     using Owin;
    

    【讨论】:

    • 非常感谢@RonC!解决了!添加了 using Owin 指令,还使用名称不同于 Configuration 的方法注册了 SignalR,以避免与自动生成的 Configuration 名称冲突VS 的财产;)
    • 酷。是哪个 using 语句?我可以编辑我的答案,然后您可以接受更新后的答案。
    • 我使用了 using Owin 指令@RonC。此目标不需要使用 Microsoft.Owin。谢谢!
    猜你喜欢
    • 2014-05-28
    • 2021-12-05
    • 2019-09-22
    • 2018-07-22
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多