HttpContext null in the Signalr Hub file
Asked Answered
T

1

8

I have a SignalR hub. When the client disconnects, I want to perform a file operation. To do that, I need to access the Server.MapPath.

However, as my Hub class is not an .aspx or a Web Service, there is no HttpContext nor request.

I thought of delegating the IO work to the HTTP request handler, but I can not create a WebRequest with a relative URI and I need to be able to deploy the solution in various locations.

How can I be able to get the local path to use in System.IO classes from a SignalR?

System.IO.Directory.GetCurrentDirectory() gives me the location of the IIS worker process.

Taxexempt answered 5/5, 2013 at 9:22 Comment(0)
V
16

You can try the following

 var path = (System.Web.HttpContext.Current == null)
                    ? System.Web.Hosting.HostingEnvironment.MapPath("~/")
                    : System.Web.HttpContext.Current.Server.MapPath("~/");

which uses the hostingenvironment to get the path of the executing application.

Vivie answered 5/5, 2013 at 10:2 Comment(2)
saravanan, this did it!! Thanks heapsTaxexempt
This works, but I believe you can simply only use HostingEnvironment.MapPath, it works in all cases. That is, the null check and the Server.MapPath line could be removed here.Quezada

© 2022 - 2024 — McMap. All rights reserved.