Khash
Khash

Reputation: 2570

Host WCF service with Castle-Windsor outside IIS with code only

I'm trying to host a WCF service inside a console app using Castle-Windsor 2.5 (.NET 4) with the following code:

        new WindsorContainer()
            .AddFacility<WcfFacility>()
            .Register(
            Component.For<IMyService>().ImplementedBy<MyService>()
                          .ActAs(new DefaultServiceModel()
                                             .AddEndpoints(
                                             WcfEndpoint.BoundTo(new BasicHttpBinding()).At("http://localhost:1010/MyService"),
                                             WcfEndpoint.BoundTo(MetadataExchangeBindings.CreateMexHttpBinding()).At("http://localhost:1010/MyService/mex"))
                                         ));

I don't have and prefer not to have any config in my app.config for WCF if possible.

This however doesn't seem to work (doesn't complain but WcfTestUtil can't see the service).

Am I missing anything?

Upvotes: 1

Views: 1566

Answers (3)

kite
kite

Reputation: 1548

I am using wcfFacility 3.3.0 and hosting wcf service dll in windows service this is the working component registration for me: (add Hosted() )

public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<LoggingFacility>(f => f.UseLog4Net());

        container
            .AddFacility<WcfFacility>(f =>
            {
                f.CloseTimeout = TimeSpan.Zero;
            });


        string baseAddress = "http://localhost:8744/TVIRecorderWcfService/";

        container.Register(

            Component
                .For<ITVIRecorderWcfService>()
                .ImplementedBy<TVIRecorderWcfService>()
                .AsWcfService(
                new DefaultServiceModel()
                    .AddBaseAddresses(baseAddress)
                    .Hosted()
                    //publish metadata doesn't work, have to do differently
                    //.PublishMetadata(x => x.EnableHttpGet()).Discoverable()
                    .AddEndpoints(WcfEndpoint
                        .BoundTo(new BasicHttpBinding()))
                        //.PublishMetadata(x=>x.EnableHttpGet()).Discoverable()
                        ).LifestyleSingleton()
                        ,

            Component
                .For<ServiceBase>()
                .ImplementedBy<TVIRecorderService>());
    }

To be seen by WcfTestClient util, the service must publish it's serviceMetadata I have to manually add serviceBehaviour and MetadataExchangeBindings after instantiating my ServiceHost

var binding = MetadataExchangeBindings.CreateMexHttpBinding();
var mexAddress = "http://localhost:8744/TVIRecorderWcfService/mex";
var behaviour = new ServiceMetadataBehavior() {HttpGetEnabled = true};


serviceHost.Description.Behaviors.Add(behaviour);
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, mexAddress);

Upvotes: 1

Mike Chamberlain
Mike Chamberlain

Reputation: 42480

Based on Khash's link from Google Groups, here is the bare minimum code to make this work:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container
        .AddFacility<WcfFacility>()
        .Register(
            Component.For<ICoreService>()
            .ImplementedBy<CoreService>()
            .AsWcfService(new DefaultServiceModel()
                .AddBaseAddresses("http://localhost:1000/core")
                .AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()))
                    .PublishMetadata(o => o.EnableHttpGet()))
    );
}

Upvotes: 1

Khash
Khash

Reputation: 2570

Posted the question on Castle Google Groups and got much better feedback but since SO is more Google friendly than Google Groups (Irony!), am going to post the link to the answer here for others: http://groups.google.com/group/castle-project-users/browse_thread/thread/d670d8f1d7aae0ab

Upvotes: 3

Related Questions