gpt4 book ai didi

wcf - 减少 WCF 配置中重复的嵌套标识

转载 作者:行者123 更新时间:2023-12-04 04:35:21 26 4
gpt4 key购买 nike

我的 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 元素中的顶级)指定了一次默认身份定义。

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

最佳答案

摘要

使用 Standard Endpoints使用适当的身份信息创建自定义端点,可以从配置文件中进行配置。

详情

谢谢你的提问!。用于减少配置开销的 WCF 服务的统一配置是我一直想研究的问题,而您的问题恰恰给了我这样做的借口。

我使用 Standard Endpoints 解决了这个问题,自 .NET 4 以来就已经存在。大部分工作是通过继承 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>
    {

    }
    }
    system.serviceModel .config 文件的部分如下所示:
    <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 将端点“标记”为具有自定义功能。属性。
  • 关于wcf - 减少 WCF 配置中重复的嵌套标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19830069/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com