作为记录,这是我最终使用的语法。(鉴于需要更多 Castle 文档,我认为任何工作语法的示例都不会受到伤害)!
Container.Kernel.Register(Component.For(serviceType)
.LifeStyle.Is(lifestyle)
.DependsOn(new
{
clientModel = new DefaultClientModel
{
Endpoint = WcfEndpoint
.FromConfiguration(serviceType.Name.Substring(1) + "Client")
}
}));
另外为了记录,ServiceSecurityContext.Current 为空的问题是出于不同的原因发生的。
我对配置文件进行了更改,我认为这无关紧要(因为某些类似乎已过时),但结果却非常重要(并且非常需要这些类)。
城堡配置文件中的“组件”节点如下所示:
<component id="FederatedServiceHostBuilder"
service="Castle.Facilities.WcfIntegration.IServiceHostBuilder`1[[Castle.Facilities.WcfIntegration.DefaultServiceModel,
Castle.Facilities.WcfIntegration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc]], Castle.Facilities.WcfIntegration"
type="OurCustomer.Framework.Service.Security.FederatedServiceHostBuilder, OurCustomer.Framework.Service"
lifestyle="Transient">
<interceptors>
<interceptor>${InstrumentationInterceptor}</interceptor>
</interceptors>
</component>
“FederatedServiceHostBuilder”类:
/// <summary>
/// The default implementation of <see cref="IServiceHostBuilder{M}"/>.
/// </summary>
public class FederatedServiceHostBuilder : AbstractServiceHostBuilder<DefaultServiceModel>
{
/// <summary>
/// Constructs a new <see cref="FederatedServiceHostBuilder"/>.
/// </summary>
/// <param name="kernel">The kernel.</param>
public FederatedServiceHostBuilder(IKernel kernel)
: base(kernel)
{
}
#region AbstractServiceHostBuilder Members
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <param name="serviceModel"></param>
/// <param name="baseAddresses"></param>
/// <returns></returns>
protected override ServiceHost CreateServiceHost(ComponentModel model,
DefaultServiceModel serviceModel,
params Uri[] baseAddresses)
{
return CreateServiceHost(model, GetEffectiveBaseAddresses(serviceModel, baseAddresses));
}
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <param name="baseAddresses"></param>
/// <returns></returns>
protected override ServiceHost CreateServiceHost(ComponentModel model,
params Uri[] baseAddresses)
{
return new FederatedServiceHost(model, baseAddresses);
}
/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <param name="baseAddresses"></param>
/// <returns></returns>
protected override ServiceHost CreateServiceHost(Type serviceType,
params Uri[] baseAddresses)
{
return new FederatedServiceHost(serviceType, baseAddresses);
}
#endregion
}
和(关键的)FederatedServiceHost 类:
public class FederatedServiceHost : Castle.Facilities.WcfIntegration.DefaultServiceHost
{
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <param name="baseAddresses"></param>
public FederatedServiceHost(ComponentModel model, params Uri[] baseAddresses)
: base(model, baseAddresses)
{
}
/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <param name="baseAddresses"></param>
public FederatedServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
/// <summary>
/// <remarks />
/// </summary>
protected override void OnOpening()
{
FederatedServiceCredentials.ConfigureServiceHost(this);
base.OnOpening();
}
}
所以最重要的是,缺少的是“FederatedServiceCredentials.ConfigureServiceHost(this)”行,因此导致我的 ServiceSecurityContext.Current 为空。