2011-01-28 73 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() 
    ); 
相關問題