Search code examples
wcfcastle-windsor

How can I register a Wcf Service contract without knowing the implementation


Without Castle Windsor I would write:

var channelFactory = new ChannelFactory<ICredentialService>("Default"); 
ICredentialService credentialService = channelFactory.CreateChannel(); 

How can I register a Wcf Service contract with the Castle Windsor API?


Solution

  • Add the Castle Windsor WCF integration facility Nuget package to your project

    Add the WcfFacility to your container:

    container.AddFacility<WcfFacility>();
    

    Then, tell the container to provide a WCF client when you have a dependency on your service interface:

    container.Register(
        Component.For<ICredentialService>()
            .AsWcfClient(WcfEndpoint.FromEndpoint("EndpointName")));
    

    It will then use the named endpoint in your .config to retrieve the settings for the endpoint.

    Whenever a class resolved from the container has a constructor dependency on ICredentialService the container will inject a WCF client.