Skip to content

Instantly share code, notes, and snippets.

@pocheptsov
Created July 30, 2015 20:22
Show Gist options
  • Save pocheptsov/8310046866b51ca6b17d to your computer and use it in GitHub Desktop.
Save pocheptsov/8310046866b51ca6b17d to your computer and use it in GitHub Desktop.
Register SoapExtension in a code
public static void RegisterSoapExtension(Type type, int priority, PriorityGroup group)
{
if (!type.IsSubclassOf(typeof(SoapExtension)))
{
throw new ArgumentException("Type must be derived from SoapException.", nameof(type));
}
if (priority < 1)
{
throw new ArgumentOutOfRangeException(nameof(priority), priority, "Priority must be greater or equal to 1.");
}
// get the current web services settings...
WebServicesSection wss = WebServicesSection.Current;
// set SoapExtensionTypes collection to read/write...
FieldInfo readOnlyField = typeof(System.Configuration.ConfigurationElementCollection).GetField("bReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
readOnlyField?.SetValue(wss.SoapExtensionTypes, false);
// inject SoapExtension...
wss.SoapExtensionTypes.Add(new SoapExtensionTypeElement(type, priority, group));
// set SoapExtensionTypes collection back to readonly and clear modified flags...
MethodInfo resetModifiedMethod = typeof(System.Configuration.ConfigurationElement).GetMethod("ResetModified", BindingFlags.NonPublic | BindingFlags.Instance);
resetModifiedMethod.Invoke(wss.SoapExtensionTypes, null);
MethodInfo setReadOnlyMethod = typeof(System.Configuration.ConfigurationElement).GetMethod("SetReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
setReadOnlyMethod.Invoke(wss.SoapExtensionTypes, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment