IApplicaionBuilder dos not contain a definition UseEndpoints. app.UseEndpoints(…) not workin on ASP.NET CORE

huangapple go评论135阅读模式
英文:

IApplicaionBuilder dos not contain a definition UseEndpoints. app.UseEndpoints(...) not workin on ASP.NET CORE

问题

我正在尝试在我的项目中使用SignalR,但当我尝试使用app.UseEndpoints(...)时,它给我一个错误,说"IApplicationBuilder does not contain UserEndpoints"。这是我的StartUp类上的代码:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseAuthentication();

    // SIGNAL R - ERROR
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<ChatHub>("/myHub");
    });

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

你应该怎么做?

我的Hub类:

public class MyHub : Microsoft.AspNet.SignalR.Hub
{
    public async Task PostMarker(string latitude, string longitude) 
    {
        await Clients.All.SendAsync("ReceiveLocation", latitude, longitude);
    }
}
英文:

I'm trying to incorporate signalR on my project, but when I try to use app.UseEndpoints(...) it gives me an error saying that "IApplicationBuilder does not contain UserEndpoints. Here is the code on my StartUp class:

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler(&quot;/Home/Error&quot;);
            app.UseHsts();
        }

       
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        //SIGNAL R - ERROR
        app.UseEndpoints(endpoints =&gt;
        {
            endpoints.MapHub&lt;ChatHub&gt;(&quot;/myHub&quot;);
        });



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

What should I do?

My Hub:

 public class MyHub : Microsoft.AspNet.SignalR.Hub
   {
    public async Task PostMarker(string latitude, string longitude) 
    {
        await Clients.All.SendAsync(&quot;ReceiveLocation&quot;, latitude, longitude);
    }
}

答案1

得分: 11

根据您的评论,您正在针对.NET Core 2.1,但UseEndpoints扩展方法是在3.0中引入的

要在2.1中添加SignalR,首先确保您的ConfigureServices方法中有services.AddSignalR();。其次,在Configure方法中,您应该使用app.UseSignalR,而不是UseEndpoints

像这样:

app.UseSignalR(route =>
{
    route.MapHub<MyHub>("/myHub");
});
英文:

As per your comment, you are targeting .NET Core 2.1, but the UseEndpoints extension method was introduced in 3.0.

To add SignalR in 2.1, firstly make sure you have services.AddSignalR(); in your ConfigureServices method. Secondly, you should use app.UseSignalR in the Configure method, instead of UseEndpoints.

Like so:

app.UseSignalR(route =&gt;
{
    route.MapHub&lt;MyHub&gt;(&quot;/myHub&quot;);
});

huangapple
  • 本文由 发表于 2020年1月3日 20:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定