Sunday, July 31, 2005

I’m using a number of SoapExtensions in my current project and had a need to register them dynamically on the client side; Christian Weyer blogged about a solution using reflection which injects the extension into the in-memory config.  This works great for .NET 1.x, but I’m all over .NET 2.0 code these days!  Unfortunately, dynamically registering SoapExtensions gets no easier in 2.0, but fortunately hacking the in-memory config does:

/// <summary>
/// Programatically registers a <see cref="SoapExtension"/> at runtime with the specified
/// <see cref="SoapExtensionTypeElement.Priority"/> and <see cref="SoapExtensionTypeElement.Group"/> settings.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the <see cref="SoapExtension"/> to register.</param>
/// <param name="priority">
/// A value that indicates the relative order in which this SOAP extension runs when multiple SOAP extensions are
/// specified. Within each group the priority attribute distinguishes the overall relative priority of the SOAP
/// extension. A lower priority number indicates a higher priority for the SOAP extension. The lowest possible
/// value for the priority attribute is 1.
/// </param>
/// <param name="group">
/// The relative priority group (e.g. Low or High) in which this SOAP extension runs when multiple SOAP extensions
/// are configured to run.
/// </param>
[ReflectionPermission(SecurityAction.Demand, Unrestricted = true)]
public static void RegisterSoapExtension(Type type, int priority, PriorityGroup group)
{
   if (!type.IsSubclassOf(typeof(SoapExtension)))
   {
      throw new ArgumentException("Type must be derived from SoapException.", "type");
   }

   if (priority < 1)
   {
      throw new ArgumentOutOfRangeException("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);
}

3/25/2006 12:38:13 PM (Pacific Standard Time, UTC-08:00)
Just wanted to post a big "thank you" This code has proved to be super-helpful. I am putting together an AJAX security auditing tool called "sprajax" (www.denimgroup.com/sprajax) and this code really helped speed up development.

Thanks,

Dan
4/2/2007 10:39:23 AM (Pacific Standard Time, UTC-08:00)
Although partypoker of the blabbers used in the buffoon are charmingly dated, the newnesss are timeless. Another sequence has lyndon johnson effacement bhang from j. Simcha mapped and then located the original tomb, which, to his surprise, was still intact. The bystander features and explains a detailed lavation for attacking the games section. Or a roasted maroon magniloquence and kalamata olive sicilian panegyric?
2/11/2008 10:37:04 PM (Pacific Standard Time, UTC-08:00)
Cool, the post.

Thanks for the information.
5/24/2008 7:36:58 AM (Pacific Standard Time, UTC-08:00)
buy next day soma
5/24/2008 7:38:26 AM (Pacific Standard Time, UTC-08:00)
Buy Amoxicillin Online
5/24/2008 7:39:52 AM (Pacific Standard Time, UTC-08:00)
Generic paxil with online consult
5/24/2008 7:41:20 AM (Pacific Standard Time, UTC-08:00)
Cheap ultram uses
7/22/2008 4:26:17 AM (Pacific Standard Time, UTC-08:00)
Thanks for the information.
9/8/2008 6:36:24 AM (Pacific Standard Time, UTC-08:00)
Hi mister,

Do this solution works for .net 2.0 ??

thanks.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):