Cannot reference custom event from web.config

社会主义新天地 提交于 2020-01-14 13:46:21

问题


I've written a custom event MyWebErrorEvent derived from WebErrorEvent located in the App_Code folder of my ASP.NET 2.0 Web Site Project. The class MyWebErrorEvent is not contained within a namespace. I've put the following into my web.config file:

<configuration>
  <system.web>
    <healthMonitoring>
      <eventMappings>
        <add name="Event1" startEventCode="0" endEventCode="2147483647" type="MyWebErrorEvent"/>
      </eventMappings>
    </healthMonitoring>
  </system.web>
</configuration>

At compile time and run time I get the following error:

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Could not load type 'MyWebErrorEvent'.

Source Error: 

Line 16: <healthMonitoring>
Line 17: <eventMappings>
Line 18: <add name="Event1" startEventCode="0" endEventCode="2147483647" type="MyWebErrorEvent"/>
Line 19: </eventMappings>
Line 20: </healthMonitoring>

The class MyWebErrorEvent does indeed compile without errors. Anyone know how to fix this?

EDIT: http://msdn.microsoft.com/en-us/library/bb398933.aspx says that this should work for custom events. A quote:

Add the assembly that contains the custom Web event implementation to the Bin subdirectory of the ASP.NET application. Alternatively, you can add the event source code file to the App_Code subdirectory.

However it also says this will not work for custom event providers:

Put the assembly that contains the custom provider implementation in the application's Bin subdirectory. You cannot put the provider source code file in the App_Code subdirectory, because the health monitoring system is configured and created before any code files in the App_Code subdirectory are compiled.

I'm guessing the documentation is actually incorrect about custom events and they must go into a assymbly distinct from the web site.


回答1:


(Just guessing here...)

For Web Sites, classes in App_Code do not get compiled until runtime. Your Web.config is being compiled before MyWebErrorEvent has had a chance to be dynamically compiled. Your best bet is to create a class library separately from the web site and reference the project from your web site. Another option is to convert to using a Web Application, which actually compiles all types into one DLL at compile time rather than emitting them dynamically at run-time.




回答2:


No need to put it in a separate DLL. Instead use

type="MyWebErrorEvent,App_Code" to reference the MyWebErrorEvent class.




回答3:


Does your type MyWebErrorEvent use a namespace?

The error message looks like a typical missing namespace issue.



来源:https://stackoverflow.com/questions/484237/cannot-reference-custom-event-from-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!