为什么Context.Request.GetHttpContext()在Hub inheritor的方法中返回null?

时间:2017-05-09 09:22:25

标签: c# signalr owin

要访问IOwinContex,我们需要致电Context.Request.GetHttpContext().GetOwinContext();

Context.Request.GetHttpContext()会返回null; 我查了Context.Request.Environment字典。它不包含HttpContextBase实例。为什么会这样?

在Windows 7应用程序上配置自托管OWIN:

using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.StaticFiles;
using Owin;

//...

private void StartSerice()
{
    WebApp.Start("http://127.0.0.1:8080/", (app) =>
    {
      app.Map("/api", map =>
      {
        map.RunSignalR(new HubConfiguration
        {
          EnableDetailedErrors = true
        });
      });

      app.UseFileServer(new FileServerOptions
      {
        EnableDefaultFiles = true,
        FileSystem = new PhysicalFileSystem("./www"),
        EnableDirectoryBrowsing = false,
        RequestPath = new PathString("")
      });
    }
}

AccountHub上课:

public class AccountHub : Hub
{
  public void TestMethod(string message)
  {
    HttpContextBase httpContextBase = Context.Request.GetHttpContext(); // is null

    //...
  }
}

0 个答案:

没有答案