WCF Self Hosting with model settings read from app.-config?

87 views
Skip to first unread message

Scott_M

unread,
Feb 11, 2015, 11:55:52 AM2/11/15
to castle-pro...@googlegroups.com
Is it possible to use Castle to self host a WCF service but use all the WCF endpoint/binding info from app.config (as oppossed to setting in code via DefaultServiceModel)?

Are there any good examples of this?

I tried the following but got InvalidOperationException

//code          
container.AddFacility<WcfFacility>();
..
..
container.Register(
                Component.For<IAuthenticationService>()
                         .ImplementedBy<AuthenticationService>()
                         .Named("AuthenticationService") 
                         .AsWcfService()
                         .LifestyleTransient()
                );

//config 
  <system.serviceModel>
    <services>
      <service name="AuthenticationService">
        <endpoint name ="AuthenticationService_NetTcp" address="net.tcp://localhost:9872/authenticationservice.svc" binding="netTcpBinding" contract="Foo.IAuthenticationService" />
      </service>
    </services>  
  </system.serviceModel>

//exception
AuthenticationService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

.Net 3.5
Castle 3.3.0
Windows 2012

thanks

scott

Paul Widner

unread,
Feb 25, 2015, 10:50:17 PM2/25/15
to castle-pro...@googlegroups.com
You are on the right track but just need a couple of tweaks.

1) When you use AsWcfService() instead of passing in a parameter it will read from the app.config.

Example from the Castle  unit tests:

using (new WindsorContainer()
.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
.Register(Component.For<UsingWindsor>()
.DependsOn(new { number = 42 })
.AsWcfService()
))
{
var client = ChannelFactory<IAmUsingWindsor>.CreateChannel(
new BasicHttpBinding(), new EndpointAddress("http://localhost:27198/UsingWindsor.svc"));
Assert.AreEqual(42, client.GetValueFromWindsorConfig());
}

2)  I recall running into your specific issue before and I believe I have to name the service in the app.config the service contract name, so in your case make "name" and "contract" both "Foo.IAuthenticationService" and then give it a try.

Scott_M

unread,
Feb 25, 2015, 11:17:45 PM2/25/15
to castle-pro...@googlegroups.com
Thanks Paul..

Here is what I did to finally get it to work as desired:

//Use this one as it will use the existing wcf infrastructure and web.config bindings.
            container.Register(
                Component.For<IAuthenticationService>()
                         .ImplementedBy<AuthenticationService>()
                         .Named("AuthenticationService") //Component name must jive with service name in web.config
                         .LifestyleTransient()
                );

AsWcfService() wonks it up for some reason.
Reply all
Reply to author
Forward
0 new messages