2011-01-28 94 views
1

我有一个客户端 - 服务器应用程序,它通过WCF进行相互通信。他们也都使用Castle Windsor来解决依赖关系。Castle Windsor WCF基于约定的客户端服务注册

我的目标是完全避免必须显式注册服务器或客户端WCF端点。 我已经通过“公约”所取得的服务器端使用下面的代码

// registers all services which implement exactly 1 [ServiceContract] 
_windsorContainer.Register(
AllTypes.FromThisAssembly().IncludeNonPublicTypes().Where(
    t => 1 == (from i in t.GetInterfaces() where i.IsDefined(typeof(ServiceContractAttribute), true) select i).Count()) 
    .Configure(c => c.LifeStyle.PerWcfSession() 
    .ActAs(new DefaultServiceModel().AddEndpoints(
     WcfEndpoint.BoundTo(new NetTcpBinding()) 
      .At("net.tcp://" + LocalAddress.ToString() + ":7601/" + c.ServiceType.Name), 
     WcfEndpoint.FromEndpoint(new UdpDiscoveryEndpoint()) 
      )) 

    ).WithService.Select(
    (Type type, Type[] baseTypes) => from i in type.GetInterfaces() where i.IsDefined(typeof(ServiceContractAttribute), true) select i 
    ) 
); 

该代码会发现在当前装配的所有类,以及任何其实现服务合同接口(由ServiceContract类型标识)将在地址“net.tcp:// localhost:7601/[service-contract-interface-name]”上注册(通过UDP发现)。

现在,我只想要方程的客户端。

通常情况下,使用城堡生成客户代理的WCF的合同,下面的代码将工作:

var model = new DefaultClientModel 
{ 
    Endpoint = WcfEndpoint.ForContract<IServiceContract>().BoundTo(new NetTcpBinding()).Discover(typeof(IServiceContract)) 
}; 

container.Register(
    Component.For<ChannelReconnectPolicy>(), 
    Castle.Facilities.WcfIntegration.WcfClient.ForChannels(model), 
); 

我想,是城堡,为所有“服务合同做这种登记'在给定的程序集中的接口 - 但AllTypes助手似乎只返回类,而不是接口(我猜它使它成为'AllClasses',而不是'AllTypes'!)... Castle可以做到这一点,语法是什么?克日什托夫? (帮助!)

谢谢!

+0

你解决了这个问题吗?我想知道完全一样的东西......好问题! +1 – Eduard 2011-09-01 09:03:03

回答

1

对于这样一个迟到的回复道歉 - 开发人员的生活从来不是一个安静的!

我挖出来的代码,它不是为“简洁”为我所希望的,也许有人可以看看城堡整合这样的事情......但在这里不用...

// these are all the namespaces that will be scanned for WCF service contracts 
string[] remoteServiceNamespaces 
    = new string[] { "MyContracts.Services", "Interlinq" }; 

// everything from here on is in the Castle DLLs (all the types) 
List<IRegistration> clientContractRegistrations = new List<IRegistration>(); 
foreach (
    var interfaceContract in 
    (from s in remoteServiceNamespaces 
    select (from i in Assembly.LoadWithPartialName(s).GetTypes() 
     where i.IsInterface 
     && 
     i.IsDefined(typeof(ServiceContractAttribute), false) 
     select i)).SelectMany(x => x) 
    ) 
{ 
    ServiceContractAttribute attr 
     = Attribute.GetCustomAttribute(
        interfaceContract, 
        typeof(ServiceContractAttribute)) 
        as ServiceContractAttribute; 
    if (null != attr) 
    { 
    WcfClientModelBase model = null; 

    // here we handle the case of the service being duplex... 
    if (null != attr.CallbackContract) 
    { 
     model = new DuplexClientModel 
     { 
     // All the automatically registered services will use NetTcp, 
     // and will discover their addresses (you could use binding 
     // inference aswell if you liked) 
     // here I have a method called 'CreateNetTcpBinding' which 
     // creates my custom binding that ALL my services use. 
     Endpoint = 
       WcfEndpoint.ForContract(interfaceContract) 
       .BoundTo(CreateNetTcpBinding()) 
       .Discover(interfaceContract) 
       .PreferEndpoint(list => list[0]) 
     }.Callback(_windsor.Resolve(attr.CallbackContract)); 
    } 
    else 
    { 
     model = new DefaultClientModel 
     { 
     Endpoint = WcfEndpoint.ForContract(interfaceContract) 
        .BoundTo(new NetTcpBinding()) 
        .Discover(interfaceContract) 
        .PreferEndpoint(list => list[0]) 
     }; 
    } 
    clientContractRegistrations.Add(WcfClient.ForChannels(model)); 
    } 
} 

// now that we've built our registration list, let's actually register 
// them all with Windsor 
_windsor.Register(
    clientContractRegistrations.ToArray() 
    ); 
相关问题