How to create a web.config file to get SoapExtensi

2019-02-22 04:11发布

问题:

I need to use a SoapExtension subclass (which I have created) but it seems that this class can only be initialized throught a "web.config" file (Though I have read that it should be possible through "app.config" file - but I don't know how to do it that way). Problem : I have no web.config file in my project. So I created one manually with the following content :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="MyNameSpace.MySoapExtension,MyAssemblyFileName" priority="1"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>
</configuration>

Despite breakpoints distpatched in each SoapExtension methods, nothing happens at runtime, it seems that it is never initialized nore called... (My SoapService initializes ok, but without any extension).

I guess that creating the web.config file manually may not be enough for it to be taken into account, so I am wondering how to properly configure my app to have a web.config file in order to use my SoapExtension (It should go and initialize my class, processMessage, and chainStream stuff...).

(Note: this is my first ever SoapExtension implementation, I am not really sure about what I am doing)

回答1:

I found out how to (or indeed 'where to') insert the web.config content in the app.config file:

I had to insert it after 'ApplicationSettings' and 'userSettings' ; this is the only place where it doesnt generate any error at runtime (So no need for a web.config file - though i still would like to know how to configure the app, if someone has the answer...).

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <applicationSettings>
    </applicationSettings>
    <userSettings>
    </userSettings>
    <system.web>
        <webServices>
          <soapExtensionTypes>
            <add type="MyNameSpace.MySoapExtension,MyAssemblyFileName" priority="1"/>
          </soapExtensionTypes>
        </webServices>
    </system.web>
    <system.serviceModel>
        <bindings />
        <client />
    </system.serviceModel>
</configuration>

My SoapExtension seems to be correctly initialized now, the message filtering works fine.



回答2:

Related to @soleshoe's comment I couldn't get my Unit tests (XUnit) to work, my App.config looked like this in the end.

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="System.Common.SOAPExtensions.TraceExtension, System.Common"  priority="1"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>
</configuration>