SignalR Core和SqlDependency的问题

时间:2018-10-25 14:25:54

标签: c# asp.net-core sqldependency asp.net-core-signalr

我正在尝试在我的Web应用程序中运行一个相当简单的SignalR Core实现。在我的解决方案中,我有一个集线器和启动文件。这就是我到目前为止所拥有的。

Startup.cs

    using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Data.SqlClient;

namespace RatingSignalR
{
  public class Startup
  {
    public Startup(IConfiguration configuration, ILogger logger)
    {
      Configuration = configuration;
      Logger = logger;
    }

    public IConfiguration Configuration { get; }
    public ILogger Logger { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
      {
        builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
      }));
      services
        .AddSignalR(options =>
        {
          options.EnableDetailedErrors = true;
        });
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
      string connStr = System.Configuration.ConfigurationManager.AppSettings["connRackley"];
      SqlDependency.Start(connStr);
    }

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

        app.UseCors("CorsPolicy");

        app.UseSignalR(builder =>
        {
          builder.MapHub<RatingSignalRHub>("ratingsignalrhub");
        });

        app.UseMvc();
      }
      catch (System.Exception e)
      {
        Logger.LogError(e.Message);
      }

    }
  }
}

RatingSignalRHub.cs

using Microsoft.AspNetCore.SignalR;
using System.Data.SqlClient;

namespace RatingSignalR
{
  public class RatingSignalRHub : Hub
  {
    public void FetchComparisons()
    {
      Clients.All.SendAsync("FetchComparisons");
    }

    void OnChange(object sender, SqlNotificationEventArgs e)
    {
      SqlDependency dep = sender as SqlDependency;
      dep.OnChange -= OnChange;
      if (e.Info != SqlNotificationInfo.Error && e.Info != SqlNotificationInfo.Delete)
      {
        FetchComparisons();
      }
    }

    void GetComparisons()
    {
      string connStr = System.Configuration.ConfigurationManager.AppSettings["connRackley"];
      SqlConnection conn = new SqlConnection(connStr);
      conn.Open();
      // do stuff
      SqlCommand cmd = new SqlCommand("uspGetPAComparisons", conn)
      {
        Notification = null
      };
      SqlDependency dep = new SqlDependency(cmd);
      dep.OnChange += new OnChangeEventHandler(OnChange);

      if (conn.State == System.Data.ConnectionState.Closed)
      {
        conn.Open();
      }
      cmd.ExecuteReader();
      conn.Close();
    }
  }
}

如果我从启动中删除了SqlDependency.Start(connStr),它就可以正常工作。但是,添加该行会导致以下错误消息: SignalR Error(对不起,还不能嵌入图像)。

有人对为什么添加SqlDependency start方法引起此问题有任何建议吗?预先感谢。

0 个答案:

没有答案