Server.MapPath in a COM component

I am rewriting the old WSCWript WSC component into a nicer C # COM component.

For some terrible reason, the old component is transferred in one place to the server context, IServer using

Set objCurr = CreateObject("MTxAS.AppServer.1") Set objCurrObjCont = objCurr.GetObjectContext() Set component.servercontext = objCurrObjCont("Server") 

then it is used to execute the standard Server.MapPath("/somelocation")

However, I do not understand what to do in the .Net COM component, System.Web.HttpContext.Current.MapPath() does not work as expected, since there is no web context.

I tried passing the context from the classic ASP to the COM component, but I'm not sure which link to include so that I can call the correct member, Microsoft.Active X Data Objects 2.7 seems to be common, but this only includes Recordsets, etc. , nothing for the C ++ IServer interface, so this just happens as a COM OBJECT .

Does anyone know how to do this / work? At this speed, I think I might have to change the behavior of the component

+4
source share
3 answers

Add the interop project for ASP.dll to your C # project (you will find it in the \ system32 \ inetsrv folder.

Add a public method to the class that ASP creates: -

  ASPTypeLibrary.ScriptingContext context; public void OnStartPage(ASPTypeLibrary.ScriptingContext sc) { context = sc; } 

Now that you need to use MapPath: -

  context.Server.MapPath("..."); 

A note context gives you access to a request, response, and session in addition to the server. OnStartPage is a preliminary COM + hack that is used by ASP and works even in the most recent versions. ASP performs the COM equivalent of reflection (by examining the information about the COM class type library) to determine if the public OnStartPage method is available if it calls it by passing a ScriptingContext object.

There is no .NET HttpContext; the request had to be processed by .NET in the first place so that it exists. HttpContext cannot be created in a post-fact stream. Therefore, if your component needs to interact with the Http conversation, it will need to do this through the ASP context object, since ASP is the host that actually processes the request.

+9
source

I think that really changing the behavior is the best option here ... hello ... not really the behavior, but the interface of the COM object ... Instead of passing in the server context, just pass the relevant information necessary for the method.

+2
source

Why not go the full path to "/ somelocation" on your C # COM component? This will help you get rid of some ugly addictions.


UPDATE: you can try HostingEnvironment.MapPath . Before use, you must add a link to System.Web.

0
source

All Articles