Results 1 to 23 of 23

Thread: Is it possible to make a dll method application independent?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'note that the dll that runs this code will need a reference to both:
    2. 'system.windows.forms and system.web in order to have access
    3. 'to the form and page types
    4.     Public Enum ApplicationTypes
    5.         Windows
    6.         Web
    7.         Other
    8.     End Enum
    9.  
    10.     Public Function GetCallingName() As String
    11.         Dim asm As Reflection.Assembly = Reflection.Assembly.GetCallingAssembly
    12.         Return asm.FullName
    13.     End Function
    14.  
    15.     Public Function GetApplicationType() As ApplicationTypes
    16.         Dim asm As Reflection.Assembly = Reflection.Assembly.GetCallingAssembly
    17.         For Each t As Type In asm.GetTypes
    18.             If t.IsSubclassOf(GetType(Windows.Forms.Form)) Then
    19.                 'has a type that is inherited from form so it is
    20.                 'probably a windows app
    21.                 Return ApplicationTypes.Windows
    22.             ElseIf t.IsSubclassOf(GetType(Web.UI.Page)) Then
    23.                 'has a type that is inherited from page so it is
    24.                 'probably a web app
    25.                 Return ApplicationTypes.Web
    26.             End If
    27.         Next
    28.         Return ApplicationTypes.Other
    29.     End Function

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Hi!

    I used this code:

    VB Code:
    1. Dim assName As [Assembly]
    2. Dim str As String = assName.GetEntryAssembly.FullName.ToString
    and it works like a charm...

    kind regards
    Henrik

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  18. #18
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  19. #19
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160

    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:
    1. Function AppendError(ByVal Sender as system.object, byVal ErrorMessage as string) as boolean
    2.  
    3. dim strAppName as string = Sender.GetType.Assembly.GetCallingAssembly.FullName
    4.  
    5. 'do your file write code
    6.  
    7. End Function
    Last edited by jwmoore2001; Jan 22nd, 2004 at 06:31 AM.
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    what does that have to do with assemblies and reflection?


    Problem solved btw, many thanks!!

    /Henrik

  21. #21
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160
    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:
    1. 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.
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

  22. #22

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  23. #23
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160
    LOL!
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width