How to get working path of a wcf application?
I needed the same information for my IIS6 hosted WCF application and I found that this worked for me: string apPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath; As always, YMMV.
I needed the same information for my IIS6 hosted WCF application and I found that this worked for me: string apPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath; As always, YMMV.
No, from MSDN on HttpContext.Current: “Gets or sets the HttpContext object for the current HTTP request.” In other words it is an HttpContext object, not a Page. You can get to the Page object via HttpContext using: Page page = HttpContext.Current.Handler as Page; if (page != null) { // Use page instance. }
It’s possible to use HostingEnvironment.MapPath() instead of HttpContext.Current.Server.MapPath() I haven’t tried it yet in a thread or timer event though. Some (non viable) solutions I considered; The only method I care about on HttpServerUtility is MapPath. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory and build my paths from this. But this will fail if … Read more
HttpContext is read-only, but it is actually derived from the ControllerContext, which you can set. controller.ControllerContext = new ControllerContext( context.Object, new RouteData(), controller );
The simplest way is to get the application, ApplicationInstance, and use its Context property: // httpContextBase is of type HttpContextBase HttpContext context = httpContextBase.ApplicationInstance.Context; (thanks to Ishmael Smyrnow who noted this in the comments) Original answer: You can, especially if the HttpContextBase instance you’ve been handed is of type HttpContextWrapper at run-time. The following example … Read more
HttpContext.Current returns an instance of System.Web.HttpContext, which does not extend System.Web.HttpContextBase. HttpContextBase was added later to address HttpContext being difficult to mock. The two classes are basically unrelated (HttpContextWrapper is used as an adapter between them). Fortunately, HttpContext itself is fakeable just enough for you do replace the IPrincipal (User) and IIdentity. The following code … Read more
You can “fake it” by creating a new HttpContext like this: http://www.necronet.org/archive/2010/07/28/unit-testing-code-that-uses-httpcontext-current-session.aspx I’ve taken that code and put it on an static helper class like so: public static HttpContext FakeHttpContext() { var httpRequest = new HttpRequest(“”, “http://example.com/”, “”); var stringWriter = new StringWriter(); var httpResponse = new HttpResponse(stringWriter); var httpContext = new HttpContext(httpRequest, httpResponse); var … Read more