Tuesday, November 6, 2012

Installing WCF-client using Castle Wcf Facility

Let's say we want to consume WCF-service in our ASP.NET MVC application. We try to keep clean design, and we want to inject our dependent WCF service, say, in our MVC Controller.

For example:
 public class MyController : Controller  
   private readonly IAnonymousService _anonymousService;  
   public MyController(IAnonymousService anonymousService)  
    _anonymousService = anonymousService;  

To accomplish this we need new version of Castle Windsor container. You need to create WindsorContainer, add Castle WCFWacility, and then register your service.

It may looks like this(in your Global.asax Application_Start handler):
 Container = new WindsorContainer();  
 var anonumousServiceUrl = ConfigurationManager.AppSettings["AnonymousServiceUrl"];  
 Container.Register(Component.For<IAnonymousService>().AsWcfClient(WcfEndpointBoundTo(new BasicHttpBinding()).At(anonumousServiceUrl)));  

No comments:

Post a Comment