ASP.NET, Class Libraries, and HTTP Handlers

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I have a Solution that contains a Web Application and a Class Library. The
Class Library contains an HTTP Handler, which implements IHttpHandler and
needs registered in the config file. I want the *.dll for the Class Library
to be available and work independently of the Web Application. The handler
works when I register the handler in the <httpHandlers> section of the
Web.config file in the Web Application, but how can I register it using the
Class Library? Thanks.
 
Nathan said:
I have a Solution that contains a Web Application and a Class Library.
The Class Library contains an HTTP Handler, which implements
IHttpHandler and needs registered in the config file. I want the *.dll
for the Class Library to be available and work independently of the Web
Application. The handler works when I register the handler in the
<httpHandlers> section of the Web.config file in the Web Application,
but how can I register it using the Class Library? Thanks.

A class library uses an app.config.
 
I did notice the app.config file in the Class Library, but when I moved the
registration from the Web Application's Web.config to the Class Library's
app.config, it didn't seem to do anything. Is there anything that needs to
be different in any way when the registration is in app.config? Thanks.
 
Nathan said:
I did notice the app.config file in the Class Library, but when I moved
the registration from the Web Application's Web.config to the Class
Library's app.config, it didn't seem to do anything. Is there anything
that needs to be different in any way when the registration is in
app.config? Thanks.

The Web.config is the root of all configs and the app.config is going to
be ignored by the Web application.

You should make a Console project and add the classlib project to the
solution and try the app.config there.
 
A DLL in Net is a Class Library not something that is independent working.

If you want the later you can use a service (or a webservice of WFC).

Success

Cor
 
Actually i created something for that ,, let me switch to my dev computer
( be back in a moment )

Michel
 
Op 7-3-2010 10:08, Michel Posseth [MCP] schreef:
Actually i created something for that ,, let me switch to my dev
computer ( be back in a moment )

Michel



and here it is :

Imports System.Configuration
Imports System.Web.Configuration
Namespace My
Partial Friend NotInheritable Class MySettings
Private DllSettings As ClientSettingsSection
Private DllConfigDoesNotExist As Boolean
Default Public Overrides Property Item(ByVal propertyName As
String) As Object
Get
Dim oValue As Object = Nothing
Try
If RunsOnWeb() Then
' do we have the value in the web config
Dim ret As String = Nothing
Try
ret =
WebConfigurationManager.AppSettings(propertyName)
Catch ex As Exception

End Try
If String.IsNullOrEmpty(ret) Then
Try
oValue =
WebConfigurationManager.ConnectionStrings(propertyName).ConnectionString
Catch ex As Exception

End Try
End If
Else
'If the .dll.config file has already been
loaded, use it to obtain the value...
If DllSettings IsNot Nothing Then
oValue =
DllSettings.Settings.Get(propertyName).Value.ValueXml.InnerXml
ElseIf Not DllConfigDoesNotExist Then
If Me.LoadDllConfigFile() Then
oValue =
DllSettings.Settings.Get(propertyName).Value.ValueXml.InnerXml
End If
End If
End If
Catch ex As Exception
End Try
Try
If oValue Is Nothing Then
oValue = MyBase.Item(propertyName)
End If
Catch ex As Exception
End Try
Return oValue
End Get
Set(ByVal value As Object)
MyBase.Item(propertyName) = value
End Set
End Property
Public Function RunsOnWeb() As Boolean
Dim strPN As String =
System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower
Return (strPN = "w3wp" OrElse strPN = "aspnet_wp")
End Function
Private Function LoadDllConfigFile() As Boolean
Dim bDllConfigLoaded As Boolean = False
Dim cfgDll As System.Configuration.Configuration
Dim cfmDllCfg As New ExeConfigurationFileMap()

Dim sAssemblyPath As String =
Reflection.Assembly.GetExecutingAssembly().Location

Dim strNamespace As String = GetType(MySettings).FullName
strNamespace = strNamespace.Substring(0,
strNamespace.IndexOf("."c))


cfmDllCfg.ExeConfigFilename = sAssemblyPath & ".config"
Try

cfgDll =
ConfigurationManager.OpenMappedExeConfiguration(cfmDllCfg,
ConfigurationUserLevel.None)

Dim csgApplicationSettings As ConfigurationSectionGroup
= cfgDll.GetSectionGroup("applicationSettings")
Me.DllSettings =
DirectCast(csgApplicationSettings.Sections(strNamespace &
".My.MySettings"), ClientSettingsSection)
bDllConfigLoaded = True

Catch ex As Exception

'bestaat niet
DllConfigDoesNotExist = True

End Try
Return bDllConfigLoaded

End Function

End Class

End Namespace
 
I probably made a bad choice of words there. What I meant was I want people
to be able to add the *.dll to their web application and use the handler
without needing to register it in their Web.config. The reason I created the
handler is because it is used by one of the controls in my class library (it
generates a *.gif image), so I need the handler to be available to the
control at a known location (unless there is some alternative way for me to
have the *.gif's generated and returned from a URL). Any ideas?
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Cor Ligthert said:
A DLL in Net is a Class Library not something that is independent working.

If you want the later you can use a service (or a webservice of WFC).

Success

Cor
 
Nathan Sokalski said:
I probably made a bad choice of words there. What I meant was I want
people to be able to add the *.dll to their web application and use the
handler without needing to register it in their Web.config. The reason I
created the handler is because it is used by one of the controls in my
class library (it generates a *.gif image), so I need the handler to be
available to the control at a known location (unless there is some
alternative way for me to have the *.gif's generated and returned from a
URL). Any ideas?
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Cor Ligthert said:
A DLL in Net is a Class Library not something that is independent
working.

If you want the later you can use a service (or a webservice of WFC).

Success

Cor


Name your file .ashx and it should work without registering.
 
I think that you are talking about a Generic Handler. I cannot use a Generic
Handler or *.ashx file because I want everything to end up being part of my
*.dll once I compile my Class Library.
 
Nathan Sokalski said:
I think that you are talking about a Generic Handler. I cannot use a
Generic Handler or *.ashx file because I want everything to end up being
part of my *.dll once I compile my Class Library.

Yes I referring to a Generic Handler.
You can still put your class that implements IHttpHandler in your dll.
I believe you would just need something similar to the following in an .ashx
file

<%@ WebHandler Language="C#" Class="YourHandler" %>

So yes you would need one item outside of the .dll
If the users of your handler have access to the bin files of their webs I
would assume they have access to the other folders also.
 
I understand that, and I would assume they do also. However, the philosophy
I am going by here is convenience; I want to only need to give the user one
file. Also, because I don't know what folders the user will have in their
Web Application or which one they will put it in, I don't know where to tell
the control that uses the handler to look for the *.ashx file.
 
Op 7-3-2010 10:08, Michel Posseth [MCP] schreef:


Actually i created something for that ,, let me switch to my dev
computer ( be back in a moment )

and here it is :

Imports System.Configuration
Imports System.Web.Configuration
Namespace My
     Partial Friend NotInheritable Class MySettings
         Private DllSettings As ClientSettingsSection
         Private DllConfigDoesNotExist As Boolean
         Default Public Overrides Property Item(ByVal propertyName As
String) As Object
             Get
                 Dim oValue As Object = Nothing
                 Try
                     If RunsOnWeb() Then
                         ' do we have the valuein the web config
                         Dim ret As String = Nothing
                         Try
                             ret =
WebConfigurationManager.AppSettings(propertyName)
                         Catch ex As Exception

                         End Try
                         If String.IsNullOrEmpty(ret) Then
                             Try
                                 oValue=
WebConfigurationManager.ConnectionStrings(propertyName).ConnectionString
                             Catch ex As Exception

                             End Try
                         End If
                     Else
                         'If the .dll.config file has already been
loaded, use it to obtain the value...
                         If DllSettings IsNot Nothing Then
                             oValue =
DllSettings.Settings.Get(propertyName).Value.ValueXml.InnerXml
                         ElseIf Not DllConfigDoesNotExist Then
                             If Me.LoadDllConfigFile() Then
                                 oValue=
DllSettings.Settings.Get(propertyName).Value.ValueXml.InnerXml
                             End If
                         End If
                     End If
                 Catch ex As Exception
                 End Try
                 Try
                     If oValue Is Nothing Then
                         oValue = MyBase.Item(propertyName)
                     End If
                 Catch ex As Exception
                 End Try
                 Return oValue
             End Get
             Set(ByVal value As Object)
                 MyBase.Item(propertyName) = value
             End Set
         End Property
         Public Function RunsOnWeb() As Boolean
             Dim strPN As String =
System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower
             Return (strPN = "w3wp" OrElse strPN = "aspnet_wp")
         End Function
         Private Function LoadDllConfigFile() As Boolean
             Dim bDllConfigLoaded As Boolean = False
             Dim cfgDll As System.Configuration.Configuration
             Dim cfmDllCfg As New ExeConfigurationFileMap()

             Dim sAssemblyPath As String =
Reflection.Assembly.GetExecutingAssembly().Location

             Dim strNamespace As String = GetType(MySettings).FullName
             strNamespace = strNamespace.Substring(0,
strNamespace.IndexOf("."c))

             cfmDllCfg.ExeConfigFilename = sAssemblyPath & ".config"
             Try

                 cfgDll =
ConfigurationManager.OpenMappedExeConfiguration(cfmDllCfg,
ConfigurationUserLevel.None)

                 Dim csgApplicationSettings As ConfigurationSectionGroup
= cfgDll.GetSectionGroup("applicationSettings")
                 Me.DllSettings =
DirectCast(csgApplicationSettings.Sections(strNamespace &
".My.MySettings"), ClientSettingsSection)
                 bDllConfigLoaded = True

             Catch ex As Exception

                 'bestaat niet
                 DllConfigDoesNotExist = True

             End Try
             Return bDllConfigLoaded

         End Function

     End Class

End Namespace

Hello:
Good code, just a point. To get if your dll is currently loaded in a
web app is better tu use:
HttpContext.Current != null
Best regards,
Oscar Acosta
 
Back
Top