|
-
Jan 19th, 2004, 09:24 AM
#1
Thread Starter
Frenzied Member
Is it possible to make a dll method application independent?
I want to write a generic method which I register as a dll
It will execute a method which is based on the configuration settings in web.config file for each web application that uses this method
do I have to pass information from the web application in the method call like:
mydllmethod(ex, appname, setting1, setting2)
OR
is there a way to write the dll method generic so it will automatically read configuration information from the webapplication that instanciate it(it's web.config)...
I hope I make some sense...
kind regards
Henrik
-
Jan 19th, 2004, 10:54 AM
#2
You should be able to refer to the the location of the calling assembly by:
Reflection.Assembly.GetCallingAssembly.Location
or
Reflection.Assembly.GetExecutingAssembly.Location
or
Reflection.Assembly.GetEntryAssembly.Location
Sorry but I forget which one will work to get the exe from the dll but its one of those.
-
Jan 19th, 2004, 11:03 AM
#3
Thread Starter
Frenzied Member
thank you! Is there any place I can read more about the reflection namespace and what it is for? To tell the truth, I had no idea about this 
kind regards
Henrik
-
Jan 19th, 2004, 11:31 AM
#4
The help files is a good start although you wont really need much knowledge of it to get what you need just that one line. Reflection 'discovers' information about objects at runtime. Once you have the path you can locate the web config file, since config files are generally relative to the dll/exe. It might be a bit different because of the way web servers handle isolation but it should be a start.
-
Jan 20th, 2004, 03:25 AM
#5
Thread Starter
Frenzied Member
What I need is the place of where the app is running and the name of the app... I used
Dim assName As [Assembly]
Dim str As String = assName.GetEntryAssembly.FullName.ToString
and it worked great for windows apps, but when using an asp.net application I only get "ASP.NET 1.1....." no matter what I tried
Do I have to use some other assembly? And how can I tell wether the caling app is a c/S or a web app?
Kind regards
Henrik
-
Jan 20th, 2004, 09:41 AM
#6
Thread Starter
Frenzied Member
Figured I will explain what I need this for...
My component will write eventlog messages to the webbserver eventlog. I need the name of the asp.net application for every time someone want to write to the eventlog... and I don't want to user to input that manually... it will work like this
serviceobject.writeventlog("message")
so there should be one method that will be used by perhaps 10 web applications running on the server.. and it should be able to write info for each and everyone of these apps... and using the app name when doing it...
I got this working with windows forms... but not with asp.net
Anyone has any solution to this problem? It is a demand, so i must do it like this... and hopefully it is possible technically... with asp.net?
kind regards
Henrik
-
Jan 21st, 2004, 01:27 PM
#7
Using GetCallingAssembly will get the dll of the web application. I'm not sure what you want to use as a name for the entry though. If the assembly name works then you should be all set. As to how to figure out if its a web, windows or other type application then there is no built-in way to tell but here is a simple function that should work in most cases:
VB Code:
'note that the dll that runs this code will need a reference to both:
'system.windows.forms and system.web in order to have access
'to the form and page types
Public Enum ApplicationTypes
Windows
Web
Other
End Enum
Public Function GetCallingName() As String
Dim asm As Reflection.Assembly = Reflection.Assembly.GetCallingAssembly
Return asm.FullName
End Function
Public Function GetApplicationType() As ApplicationTypes
Dim asm As Reflection.Assembly = Reflection.Assembly.GetCallingAssembly
For Each t As Type In asm.GetTypes
If t.IsSubclassOf(GetType(Windows.Forms.Form)) Then
'has a type that is inherited from form so it is
'probably a windows app
Return ApplicationTypes.Windows
ElseIf t.IsSubclassOf(GetType(Web.UI.Page)) Then
'has a type that is inherited from page so it is
'probably a web app
Return ApplicationTypes.Web
End If
Next
Return ApplicationTypes.Other
End Function
-
Jan 22nd, 2004, 02:12 AM
#8
Thread Starter
Frenzied Member
Thank you... but what I really was looking for was a way to get the name of the application that is executing the dll
When I tried it under windows forms I got a nice "WindowsApplication7" as the name!, but in asp.net all I got was "ASP.NET 1.1... bla".. I wanted "WebApplication7" or whatever name it has...
Kind regards
Henrik
-
Jan 22nd, 2004, 02:25 AM
#9
I don't know that that information exists once its compiled. What code did you use to get it in Windows apps?
Of course this could easily be done with attributes if it is meant to only work with your own creations.
-
Jan 22nd, 2004, 02:27 AM
#10
Thread Starter
Frenzied Member
Hi!
I used this code:
VB Code:
Dim assName As [Assembly]
Dim str As String = assName.GetEntryAssembly.FullName.ToString
and it works like a charm...
kind regards
Henrik
-
Jan 22nd, 2004, 02:39 AM
#11
How come I don't get something so simple?
I get:
TestBed8, Version=1.0.1481.42605, Culture=neutral, PublicKeyToken=null
Which is the same basic thing I get from the web app only for the web app you'll notice I used callingassembly instead of entryassembly.
-
Jan 22nd, 2004, 02:46 AM
#12
Thread Starter
Frenzied Member
Yeah, I removed the other parameters with simple string manipulation.... but it was the first name that was interesting to me 
When I do it with callingassembly I get the name of the class in my dll... not the application class that uses the dll.
kind regards
Henrik
-
Jan 22nd, 2004, 02:49 AM
#13
In both cases you should be getting the name of the assembly. With exes this is easy enough since the name of the exe is the application name. In a web application the dll is whatever it is set to be in the project properties and isn't as relevent since it isn't needed to execute the web. What is the web application name that you seek defined? Couldn't you just change the name of the assembly?
For instance my test subject returns isharecode.web for the assembly for my web application. I'm sure if you can figure out where the application name is defined or what is considered the application name for a web app then we can find it in the assembly.
Last edited by Edneeis; Jan 22nd, 2004 at 02:54 AM.
-
Jan 22nd, 2004, 03:05 AM
#14
Thread Starter
Frenzied Member
Maybe I am doing this the wrong way, in terms of good architecture?
Consider this:
I have a class A that is performing system operations(logging of errors, etc) for many web applications. This class is in a dll registered on the web server. The class A has a method MethodA() that can be used by all the classes in the webapplications.
Webapplication B is having an exception in one of it's classes, and I want to pass that exception to MethodA(ex). MethodA() is application independent, so it must know the name of the application that this exception came from, because it is going to write a message in the application eventlog with the "source" as the name of the application.
I have thought about this problem with the GRASP patterns as support... and the Expert says clearly that a task should be assigned to the class that has the most information to fulfill the task.
That means that the base class in webapplication B should report this instead, because it has every knowledge about the exception, and it knows it's own name.
But that doesn't make it very generic... That is why I needed a class specialized in reporting exception errors in many ways (MQ, email, eventlog etc). But this class NEED to know WHO the exception is coming from (e.g. the name of the app that threw the exception...
I have looked at the Exception Application Block, and it doesn't make it so much better, it relies on some heavy setup in the web.config file to make custom error reporting. I need a slim and easy to use class...
anyone can help with this? Both in terms of coding and architecture?
kind regards
Henrik
-
Jan 22nd, 2004, 03:13 AM
#15
I think your idea of a quick and easy generic method is good. The only problem is that what defines an 'Application' or more so an 'Application Name'? Generically an application is just one or more assemblies but the assemblies only know their own name not the name of all parties involved. So it might be acceptable to just use the assembly name of the assembly that is throwing the exception. Otherwise the quickest thing I can think of is to make a custom attribute that has the desired name and use apply it to at least one class in each assembly that defines an application. You can then search the assembly for the attribute and use that name.
I have a generic error logging utility that just creates a log file in the application folder, applicationname.log.
-
Jan 22nd, 2004, 03:23 AM
#16
Thread Starter
Frenzied Member
I had another idea (borrowed from Msoft). Is it possible in a dll to get the path from where the calling assembly is located? We deploy our webb applications the same way every time
c:\inetpub\wwwroot\appname\......
If I could get that, I could open the config.web of the application and read the appname from a key there. (it is xml so it would be fairly easy to do with standard xml classes)
I have talked my fellow devs, and the one thing they are NOT interested in, is having to write the app name in every class that need to use this logging component... they just want to:
ExceptionManager.Log(ex) (log is a shared method)
... and everything else should be configured elsewhere.... (preferrably with some simple keys in web.config)
That means the log method have to gain knowledge of an app name, and the name should be unique for the webapp, because we use PATROL software to monitor our web applications, and it will be configured to alert based on SOURCE field in the eventlog, and SOURCE field=web app name...
kind regards
Henrik
-
Jan 22nd, 2004, 03:29 AM
#17
Actually only one class per assembly would need the attribute but if the web.config is easier then certainly you can get the path via the asm.Location property. I believe anyway, unless IIS does something funky for isolation.
-
Jan 22nd, 2004, 03:36 AM
#18
Actually on second thought you don't even need that. Any executing dlls automatically reference the config file of the calling assembly. So if you use anything in the Configuration namespace it should automatically pull from the web/windows app/web.config file even though it is being executed from an external dll.
-
Jan 22nd, 2004, 06:27 AM
#19
Addicted Member
Get calling dll assembly info
I have a fully functionally error handler just as you described you want to build. I use the following to get the calling assembly's root namespace.
VB Code:
Function AppendError(ByVal Sender as system.object, byVal ErrorMessage as string) as boolean
dim strAppName as string = Sender.GetType.Assembly.GetCallingAssembly.FullName
'do your file write code
End Function
Last edited by jwmoore2001; Jan 22nd, 2004 at 06:31 AM.
-
Jan 22nd, 2004, 07:32 AM
#20
Thread Starter
Frenzied Member
what does that have to do with assemblies and reflection?
Problem solved btw, many thanks!!
/Henrik
-
Jan 22nd, 2004, 08:06 AM
#21
Addicted Member
what does that have to do with assemblies and reflection?
Did you not want to create an object that handles error logging from any web application running? If so when you create a class library write a function as i described earlier and from the web application you create the reference to the dll, instantiate the object and call the function:
VB Code:
bSuccess = ErrorHandler.AppendError(Me,"This is an error message")
This passes the object calling the function, and the object extracts the assemblies application name that called the function with this line "Sender.GetType.Assembly.GetCallingAssembly.FullName" then writes the error to a file.
-
Jan 22nd, 2004, 08:07 AM
#22
Thread Starter
Frenzied Member
Yeah Im so sorry.... I wrote that as a comment to some porno link that someone wrote in this thread(that is gone now)... your post was very interesting and good!
thanks
Henrik
-
Jan 22nd, 2004, 08:14 AM
#23
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|