Asp.net Can';t从服务器调用客户端方法

Asp.net Can';t从服务器调用客户端方法,asp.net,asp.net-core,signalr,Asp.net,Asp.net Core,Signalr,我正在尝试使用signar将消息从服务器广播到客户端,而不让客户端触发消息。根据我所看到的教程,在客户端中定义方法,如下所示: signalRConnection.client.addNewMessage = function(message) { console.log(message); }; 应允许在服务器上使用以下集线器代码: public async Task SendMessage(string message) { await Clients.All.addNewM

我正在尝试使用signar将消息从服务器广播到客户端,而不让客户端触发消息。根据我所看到的教程,在客户端中定义方法,如下所示:

signalRConnection.client.addNewMessage = function(message) {
  console.log(message);
};
应允许在服务器上使用以下集线器代码:

public async Task SendMessage(string message)
{
     await Clients.All.addNewMessage("Hey from the server!");
}
但是,
Clients.All.addNewMessage
调用会在C#编译器中导致错误:

“IClientProxy”不包含“addNewMessage”的定义,并且找不到接受“IClientProxy”类型的第一个参数的可访问扩展方法“addNewMessage”(是否缺少using指令或程序集引用?)


我该如何解决这个问题?服务器代码包含在集线器中。

这是因为您使用的是ASP.NET核心信号器,但您是在ASP.NET MVC信号器之后调用客户端方法。在ASP.NET核心信号器中,必须按如下方式调用客户端方法:

public async Task SendMessage(string message)
{
     await Clients.All.SendAsync("AddNewMessage", message); // here `AddNewMessage` is the method name in the client side.
}
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

connection.on("AddNewMessage", function (message) {
    // do whatever you want to do with `message`
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSignalR(); // Must add this
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub"); // Here is configuring for `ChatHub`
        });

        app.UseMvc();
    }
}
它显示的客户端代码也是针对ASP.NET MVC Signaler的。对于ASP.NET核心信号器,应如下所示:

public async Task SendMessage(string message)
{
     await Clients.All.SendAsync("AddNewMessage", message); // here `AddNewMessage` is the method name in the client side.
}
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

connection.on("AddNewMessage", function (message) {
    // do whatever you want to do with `message`
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSignalR(); // Must add this
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub"); // Here is configuring for `ChatHub`
        });

        app.UseMvc();
    }
}
启动
信号机
中,设置应如下所示:

public async Task SendMessage(string message)
{
     await Clients.All.SendAsync("AddNewMessage", message); // here `AddNewMessage` is the method name in the client side.
}
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

connection.on("AddNewMessage", function (message) {
    // do whatever you want to do with `message`
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSignalR(); // Must add this
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub"); // Here is configuring for `ChatHub`
        });

        app.UseMvc();
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();//必须添加此
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.usesigner(路由=>
{
routes.MapHub(“/chatHub”);//下面是对“chatHub”的配置`
});
app.UseMvc();
}
}

如果您面临进一步的问题,请遵循本教程。

这是因为您使用的是ASP.NET核心信号器,但您是在ASP.NET MVC信号器之后调用客户端方法。在ASP.NET核心信号器中,必须按如下方式调用客户端方法:

public async Task SendMessage(string message)
{
     await Clients.All.SendAsync("AddNewMessage", message); // here `AddNewMessage` is the method name in the client side.
}
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

connection.on("AddNewMessage", function (message) {
    // do whatever you want to do with `message`
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSignalR(); // Must add this
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub"); // Here is configuring for `ChatHub`
        });

        app.UseMvc();
    }
}
它显示的客户端代码也是针对ASP.NET MVC Signaler的。对于ASP.NET核心信号器,应如下所示:

public async Task SendMessage(string message)
{
     await Clients.All.SendAsync("AddNewMessage", message); // here `AddNewMessage` is the method name in the client side.
}
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

connection.on("AddNewMessage", function (message) {
    // do whatever you want to do with `message`
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSignalR(); // Must add this
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub"); // Here is configuring for `ChatHub`
        });

        app.UseMvc();
    }
}
启动
信号机
中,设置应如下所示:

public async Task SendMessage(string message)
{
     await Clients.All.SendAsync("AddNewMessage", message); // here `AddNewMessage` is the method name in the client side.
}
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

connection.on("AddNewMessage", function (message) {
    // do whatever you want to do with `message`
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSignalR(); // Must add this
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub"); // Here is configuring for `ChatHub`
        });

        app.UseMvc();
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();//必须添加此
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.usesigner(路由=>
{
routes.MapHub(“/chatHub”);//下面是对“chatHub”的配置`
});
app.UseMvc();
}
}

如果您面临进一步的问题,请遵循本教程。

我敢肯定您正在阅读旧教程。您使用的是什么版本的信号器?对于asp.net核心信号器,请尝试
等待客户端.All.SendAsync(“ReceiveMessage”,user,message)将消息从服务器发送到客户端
ReceiveMessage
是客户端中定义的方法。接下来,我使用的是SignalR Core——看起来我确实在看旧的教程。我单独解决了这个问题,但我想如果这个方法在SignalR Core中有效的话,我会保留这个问题,但似乎不是这样,我会接受下面的答案。谢谢大家!我很确定你在看老教程。您使用的是什么版本的信号器?对于asp.net核心信号器,请尝试
等待客户端.All.SendAsync(“ReceiveMessage”,user,message)将消息从服务器发送到客户端
ReceiveMessage
是客户端中定义的方法。接下来,我使用的是SignalR Core——看起来我确实在看旧的教程。我单独解决了这个问题,但我想如果这个方法在SignalR Core中有效的话,我会保留这个问题,但似乎不是这样,我会接受下面的答案。谢谢大家!