0

カスタム メッセージ用に CustomBehaviorExtensionElement と CustomEndpointBehavior を作成しました。

public class CustomEndpointBehavior : IEndpointBehavior
{
    private X509Certificate2 certificate;
    public X509Certificate2 Certificate { get => certificate; set => certificate = value; }

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}
public class CustomBehaviorExtensionElement : BehaviorExtensionElement
{
    private ConfigurationPropertyCollection properties;

    [ConfigurationProperty("Certificate")]
    public X509CertificateConfigurationElement Certificate
    {
        get
        {
            return (X509CertificateConfigurationElement)base["Certificate"];
        }
    }
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            if (this.properties == null)
            {
                this.properties = new ConfigurationPropertyCollection
                {
                    new ConfigurationProperty("Certificate", typeof(X509CertificateConfigurationElement), null, null, null, ConfigurationPropertyOptions.None),
                };
            }
            return this.properties;
        }
    }

    public override Type BehaviorType
    {
        get { return typeof(CustomEndpointBehavior); }
    }

    protected override object CreateBehavior()
    {
        // Create the  endpoint behavior that will insert the message  
        // inspector into the client runtime  
        return new CustomEndpointBehavior();
    }
}
public class X509CertificateConfigurationElement : ConfigurationElement
{
    private ConfigurationPropertyCollection properties;
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            if (this.properties == null)
            {
                this.properties = new ConfigurationPropertyCollection
                {
                    new ConfigurationProperty("findValue", typeof(string), string.Empty, null, new StringValidator(0, 2147483647, null), ConfigurationPropertyOptions.None),
                    new ConfigurationProperty("storeLocation", typeof(StoreLocation), StoreLocation.CurrentUser, null, null, ConfigurationPropertyOptions.None),
                    new ConfigurationProperty("storeName", typeof(StoreName), StoreName.My, null, null, ConfigurationPropertyOptions.None),
                    new ConfigurationProperty("x509FindType", typeof(X509FindType), X509FindType.FindBySubjectDistinguishedName, null, null, ConfigurationPropertyOptions.None)
                };
            }
            return this.properties;
        }
    }

    [ConfigurationProperty("findValue", DefaultValue = ""), StringValidator(MinLength = 0)]
    public string FindValue
    {
        get
        {
            return (string)base["findValue"];
        }
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                value = string.Empty;
            }
            base["findValue"] = value;
        }
    }

    [ConfigurationProperty("storeLocation", DefaultValue = StoreLocation.CurrentUser)]
    public StoreLocation StoreLocation
    {
        get
        {
            return (StoreLocation)base["storeLocation"];
        }
        set
        {
            base["storeLocation"] = value;
        }
    }

    [ConfigurationProperty("storeName", DefaultValue = StoreName.My)]
    public StoreName StoreName
    {
        get
        {
            return (StoreName)base["storeName"];
        }
        set
        {
            base["storeName"] = value;
        }
    }

    [ConfigurationProperty("x509FindType", DefaultValue = X509FindType.FindBySubjectDistinguishedName)]
    public X509FindType X509FindType
    {
        get
        {
            return (X509FindType)base["x509FindType"];
        }
        set
        {
            base["x509FindType"] = value;
        }
    }
}

しかし、SVCEditor では、クラス X509CertificateConfigurationElement のネストされたプロパティが表示されません。SVCEditor は Type 名のみを表示しますが、ネストされたプロパティ findValue、storeLocation、storeName、x509FindType を期待していました。 ここに例 があります。回答ありがとうございます。

4

1 に答える 1

0

TypeConverter クラスの問題を解決します。カスタム TypeConverter を作成しました

public class X509CertificateConverter : ExpandableObjectConverter
{
    public override object ConvertTo(ITypeDescriptorContext context,
                         System.Globalization.CultureInfo culture,
                         object value, Type destType)
    {
        if (destType == typeof(string) && value is X509CertificateConfigurationElement)
        {
            X509CertificateConfigurationElement cert = (X509CertificateConfigurationElement)value;
            return "Certificate";
        }
        return base.ConvertTo(context, culture, value, destType);
    }
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        if (value is X509CertificateConfigurationElement)
        {
            X509CertificateConfigurationElement cert = (X509CertificateConfigurationElement)value;

            PropertyDescriptorCollection allProps = TypeDescriptor.GetProperties(cert);

            PropertyDescriptor[] propertyDescriptor = new PropertyDescriptor[4];
            propertyDescriptor[0] = allProps["FindValue"];
            propertyDescriptor[1] = allProps["StoreLocation"];
            propertyDescriptor[2] = allProps["StoreName"];
            propertyDescriptor[3] = allProps["X509FindType"];

            return new PropertyDescriptorCollection(propertyDescriptor);
        }
        return base.GetProperties(context, value, attributes);
    }
}

そして、ConfigurationElementでそれを使用します

[TypeConverter(typeof(X509CertificateConverter))]
public class X509CertificateConfigurationElement : ConfigurationElement
{
    private ConfigurationPropertyCollection properties;
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            if (this.properties == null)
            {
                this.properties = new ConfigurationPropertyCollection
                {
                    new ConfigurationProperty("findValue", typeof(string), string.Empty, null, new StringValidator(0, 2147483647, null), ConfigurationPropertyOptions.None),
                    new ConfigurationProperty("storeLocation", typeof(StoreLocation), StoreLocation.CurrentUser, null, null, ConfigurationPropertyOptions.None),
                    new ConfigurationProperty("storeName", typeof(StoreName), StoreName.My, null, null, ConfigurationPropertyOptions.None),
                    new ConfigurationProperty("x509FindType", typeof(X509FindType), X509FindType.FindBySubjectDistinguishedName, null, null, ConfigurationPropertyOptions.None)
                };
            }
            return this.properties;
        }
    }

    [ConfigurationProperty("findValue", DefaultValue = ""), StringValidator(MinLength = 0)]
    public string FindValue
    {
        get
        {
            return (string)base["findValue"];
        }
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                value = string.Empty;
            }
            base["findValue"] = value;
        }
    }

    [ConfigurationProperty("storeLocation", DefaultValue = StoreLocation.CurrentUser)]
    public StoreLocation StoreLocation
    {
        get
        {
            return (StoreLocation)base["storeLocation"];
        }
        set
        {
            base["storeLocation"] = value;
        }
    }

    [ConfigurationProperty("storeName", DefaultValue = StoreName.My)]
    public StoreName StoreName
    {
        get
        {
            return (StoreName)base["storeName"];
        }
        set
        {
            base["storeName"] = value;
        }
    }

    [ConfigurationProperty("x509FindType", DefaultValue = X509FindType.FindBySubjectDistinguishedName)]
    public X509FindType X509FindType
    {
        get
        {
            return (X509FindType)base["x509FindType"];
        }
        set
        {
            base["x509FindType"] = value;
        }
    }
}

これが私の結果ですクリック

于 2018-02-02T07:17:07.000 に答える