2

我的 WCF 应用程序的 web.config 定义了一系列端点,如下所示:

<system.serviceModel>
    <services>
        <service behaviorConfiguration="whatever" name="MyService">
            <endpoint name="Endpoint1" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract1">
                <identity>
                    <certificateReference findValue="cert name storeName="TrustedPeople" storeLocation="LocalMachine" x509FindType="FindBySubjectName" />
                </identity>
            </endpoint>
            <endpoint name="Endpoint2" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract2">
                <identity>
                    <certificateReference findValue="cert name storeName="TrustedPeople" storeLocation="LocalMachine" x509FindType="FindBySubjectName" />
                </identity>
            </endpoint>
            <endpoint name="Endpoint3" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract3">
                <identity>
                    <certificateReference findValue="cert name storeName="TrustedPeople" storeLocation="LocalMachine" x509FindType="FindBySubjectName" />
                </identity>
            </endpoint>
            <endpoint name="Endpoint4" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract4">
                <identity>
                    <certificateReference findValue="cert name storeName="TrustedPeople" storeLocation="LocalMachine" x509FindType="FindBySubjectName" />
                </identity>
            </endpoint>

我想做的是

<system.serviceModel>
    <services>
        <service behaviorConfiguration="whatever" name="MyService">
            <endpoint name="Endpoint1" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract1" />
            <endpoint name="Endpoint2" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract2" />
            <endpoint name="Endpoint3" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract3" />
            <endpoint name="Endpoint4" address="" binding="customBinding" bindingConfiguration="HttpIssuedTokenBinding" contract="My.App.Contract4" />

在另一个地方指定了一次默认身份定义(甚至只是 system.serviceModel 元素中的顶级)。

基本上我想干,因为配置自始至终都是一致的。我需要 SO 的帮助是在哪里可以找到“所有端点的默认身份”配置元素。MSDN 没有提供太多帮助,我不确定在哪里反映 .NET 库以查看在应用程序启动时读取 web.configs 时如何解释它。

4

1 回答 1

3

概括

使用标准端点创建具有适当身份信息的自定义端点,可以从配置文件进行配置。


细节

感谢你的提问!。WCF 服务的统一配置以减少配置开销是我一直想要研究的东西,而您的问题正好给了我这样做的借口。

我使用自 .NET 4 以来一直存在的Standard Endpoints解决了这个问题。大部分工作是通过继承来完成的StandardEndpointElement

namespace WcfEx
{
    public class X509EndpointElement : StandardEndpointElement
    {
        private static string _findValueKey = "findValue";
        private static string _storeNameKey = "storeName";
        private static string _storeLocationKey = "storeLocation";
        private static string _x509FindTypeKey = "x509SearchType";

        public virtual string FindValue
        {
            get { return base[_findValueKey] as string; }
            set { base[_findValueKey] = value; }
        }

        public virtual StoreName StoreName
        {
            get { return this[_storeNameKey] is StoreName ? (StoreName) this[_storeNameKey] : (StoreName) 0; }
            set { this[_storeNameKey] = value; }
        }

        public virtual StoreLocation StoreLocation
        {
            get
            {
                return this[_storeLocationKey] is StoreLocation
                           ? (StoreLocation) this[_storeLocationKey]
                           : (StoreLocation) 0;
            }
            set { this[_storeLocationKey] = value; }
        }

        public virtual X509FindType X509FindType
        {
            get { return this[_x509FindTypeKey] is X509FindType ? (X509FindType) this[_x509FindTypeKey] : (X509FindType) 0; }
            set { this[_x509FindTypeKey] = value; }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                ConfigurationPropertyCollection properties = base.Properties;
                properties.Add(new ConfigurationProperty(_findValueKey, typeof (string), null,
                                                         ConfigurationPropertyOptions.None));
                properties.Add(new ConfigurationProperty(_storeNameKey, typeof (StoreName), null,
                                                         ConfigurationPropertyOptions.None));
                properties.Add(new ConfigurationProperty(_storeLocationKey, typeof (StoreLocation), null,
                                                         ConfigurationPropertyOptions.None));
                properties.Add(new ConfigurationProperty(_x509FindTypeKey, typeof (X509FindType), null,
                                                         ConfigurationPropertyOptions.None));
                return properties;
            }
        }

        protected override Type EndpointType
        {
            get { return typeof (ServiceEndpoint); }
        }

        protected override ServiceEndpoint CreateServiceEndpoint(ContractDescription contract)
        {
            return new ServiceEndpoint(contract);
        }

        protected override void OnApplyConfiguration(ServiceEndpoint endpoint,
                                                     ServiceEndpointElement serviceEndpointElement)
        {
            endpoint.Address = new EndpointAddress(endpoint.Address.Uri,
                                                   EndpointIdentity.CreateX509CertificateIdentity(
                                                       GetCertificateFromStore()));
        }

        protected override void OnApplyConfiguration(ServiceEndpoint endpoint,
                                                     ChannelEndpointElement channelEndpointElement)
        {
            endpoint.Address = new EndpointAddress(endpoint.Address.Uri,
                                                   EndpointIdentity.CreateX509CertificateIdentity(
                                                       GetCertificateFromStore()));
        }

        private X509Certificate2 GetCertificateFromStore()
        {
            var certificateStore = new X509Store(StoreName, StoreLocation);
            certificateStore.Open(OpenFlags.ReadOnly);
            var matchingCertificates = certificateStore.Certificates.Find(X509FindType, FindValue, false);

            X509Certificate2 matchingCertificate = null;
            if (matchingCertificates.Count > 0)
                matchingCertificate = matchingCertificates[0];
            else throw new InvalidOperationException("Could not find specified certificate");

            certificateStore.Close();
            return matchingCertificate;
        }

        protected override void OnInitializeAndValidate(ChannelEndpointElement channelEndpointElement)
        {

        }

        protected override void OnInitializeAndValidate(ServiceEndpointElement serviceEndpointElement)
        {

        }
    }

}

对上述代码的快速总结:

  • 这个类可以在 .config 文件中可见——所以它的属性可以通过配置来设置;
  • 有四个属性指定选择X509证书的参数;
  • 在此类的生命周期中的某个时刻,端点地址设置为由满足搜索条件的证书指定的身份。

您需要一个集合类来保存上述类的元素:

namespace WcfEx
{
    public class X509EndpointCollectionElement : StandardEndpointCollectionElement<ServiceEndpoint, X509EndpointElement>
    {

    }
}

.config 文件的system.serviceModel部分如下所示:

<system.serviceModel>
<extensions>
  <endpointExtensions>
    <add name="x509Endpoint" type="WcfEx.X509EndpointCollectionElement, WcfEx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </endpointExtensions>
</extensions>
<standardEndpoints>
  <x509Endpoint> 
    <standardEndpoint storeLocation="LocalMachine" storeName="TrustedPeople" findValue="cert name" x509SearchType="FindBySubjectName"/>
  </x509Endpoint>
</standardEndpoints> 
<services>
  <service name="WcfHost.Service1">
    <endpoint address="" binding="wsHttpBinding" contract="WcfHost.IService1" kind="x509Endpoint" >
    </endpoint>
  </service>
</services> 
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

注意事项:

  • 此处的属性值要非常小心type——它需要与typeof (X509EndpointElement).AssemblyQualifiedName.
  • 注册后,我们可以将相关配置添加到standardEndpoints元素中。
  • kind我们通过使用属性将端点“标记”为具有自定义。
于 2013-11-19T15:08:15.440 回答