SoapExtension configuration
Today I had an interesting chat with Christian Weyer about SoapExtension configuration. My question was pretty simple: "I configured my SoapExtension both on web.config and as WebMetod attribute. Why ProcessMessage is executed twice for every SoapMessageStage ?"
When a web service is executed the first time, ASP.NET loads all the extensions both from the web.config (see section soapExtensionTypes) and the extension attribute (inhertoed from SoapExtensionAttribute) applied to the WebMethod and then process all of them. The scope of the extensions configured into the web.config is global over all WebMethods of the web application, whereas the scope of the attribute is local to the single WebMethod.
If you configure the same SoapExtension both on web.config and decorating the WebMethod with the attribute you will execute the same SoapExtension twice. What changes between these two configuration is the initialization process. When you configure in the web.config, it is called object GetInitializer(Type) where the Type is the type of the web service currently executed. If you configure the extension only by attribute, object GetInitializer(LogicalMethodInfo, SoapExtensionAttribute) get called, and contains both the information (at metadata level) about the WebMethod called and the SoapExtensionAttribute applied to it. Note that these methods are called only once and then they are cached.
The advantage in configuring a SoapExtension into the web.config is for scoping (consider for example a SOAP message tracing system), you don't have to configure each WebMethod. If you need to set some parameters instead, the attribute way is the right one.