I have this line of code in an assembly:
HttpContext.Current.Server.MapPath
This works great if the assembly is being used in a web service.
but if I take it out of the web service its obivously not going to work, because HTTPContext does not exist.
Is it at all possible to fool httpContext into thinking it exists, really just to get the relative path structure of a directory?
I mean somehow manually creating the HTTPContext object, and assigning it a base directory?
Update
Is there a more generic approach to : HttpContext.Current.Server.MapPath
Something that can work in executables, and something that can work in web?
HttpContext.Current "gets or sets the HttpContext object". And HttpContext has a public constructor which takes a HttpWorkerRequest (abstract, use SimpleWorkerRequest) and you would be able to fake the HttpContext completely.
I agree with others that you could refactor your code to remove this dependency, but there are scenarios when you must fake the HttpContext.Current stuff, in which case this question will show up if you search...
It sounds to me like you need to wrap up the call to HttpContext inside another object which you can then switch out using IOC for the correct object in the correct environment. E.g. something like IMapPath as your interface and then your implementation could be HttpMapPath or ConsoleAppMapPath etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With