Castle WCF Client Registration syntax

十年热恋 提交于 2019-12-08 10:52:10

问题


Trying to register a WCF client with Castle WcfIntegration 3.0; is there anything wrong with the following syntax?

Container.Kernel.Register( Component.For(serviceType) .AsWcfClient(new DefaultClientModel { Endpoint = WcfEndpoint .FromConfiguration( serviceType.Name. Substring(1) + "Client") }) .LifeStyle.Is(lifestyle));

The problem I'm having is when in the context of a WCF service operation, ServiceSecurityContext.Current is null. This did not happen in the old version of Castle (1.0.3.0). Trying to understand if it's something I'm doing wrong or if some change to Castle mandates some other change to get ServiceSecurityContext.Current to populate as it did before.

thanks


回答1:


For the record, here is the syntax I used that worked in the end. (Given the need for more Castle documentation, I figure any example of working syntax couldn't hurt)!

Container.Kernel.Register(Component.For(serviceType)
            .LifeStyle.Is(lifestyle)
            .DependsOn(new
            {
                clientModel = new DefaultClientModel
                {
                    Endpoint = WcfEndpoint
                    .FromConfiguration(serviceType.Name.Substring(1) + "Client")
                }
            }));

Also for the record, the problem of the ServiceSecurityContext.Current being null was happening for a different reason.

I had made a change to a config file, which I thought was insignificant (because certain classes appeared to be obsolete), but which turned out to be very significant (and those classes were very much needed).

The "component" node in the castle config file looked like this:

<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>

The "FederatedServiceHostBuilder" class:

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

and the (crucial) FederatedServiceHost class:

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

So the bottom line is, it was the "FederatedServiceCredentials.ConfigureServiceHost(this)" line that was missing, and thus causing my ServiceSecurityContext.Current to be null.



来源:https://stackoverflow.com/questions/12105622/castle-wcf-client-registration-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!