【问题标题】:Catching SOAP in C#, in a DLL在 C# 中,在 DLL 中捕获 SOAP
【发布时间】:2020-03-24 17:21:02
【问题描述】:

我正在构建一个 DLL,它应该由我正在构建的其他一些应用程序使用。 DLL 功能的一部分是记录所有 SOAP 事务。

我找到了有关如何在 C# 中捕获 SOAP 事务的说明。 https://web.archive.org/web/20160402053148/http://blog.encoresystems.net/articles/how-to-capture-soap-envelopes-when-consuming-a-web-service.aspx

我的问题是它需要将以下内容添加到 app.config(链接页面的末尾)。

<system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="Encore.PayPal.Soap.TraceExtension, Encore.PayPal.Soap" priority="1" group="0" />
      </soapExtensionTypes>
    </webServices>
  </system.web>

我不希望它成为所有使用我的 DLL 将其添加到 app.config 的要求。

有没有办法在加载 DLL 时即时执行此操作?

【问题讨论】:

    标签: c# soap app-config


    【解决方案1】:

    我在 MSDN 论坛上发现了一个类似的问题:Programmatically registering a SOAP Extension

    /// <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);  
    } 
    

    您需要在实例化客户端/服务器之前调用此方法。

    RegisterSoapExtension(typeof(TraceExtension), 1, PriorityGroup.Low);
    PaypalClient cli = new PaypalClient();
    

    【讨论】:

    • 谢谢,这正是我想要的。
    • 第2步,我不知道这是不是你自己尝试过的。但如果你这样做了,你知道这是否是线程安全的。意思是,如果我有 2 个线程并且我在其中一个中运行这个“注册”。他会捕获从第二个线程生成的 SOAP 吗?
    • 我不认为它是线程安全的。您可以将其与lock(obj) { ... } 同步,或者在启动线程之前执行此操作。
    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多