0
votes

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

1 Answers

0
votes

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.