2012-05-28 141 views
1

我想在我的WCF项目中使用城堡WCF集成设施,但迷路了。你能帮我解决吗?城堡WCF设施| Windows服务托管

所以,这里的情况是:

我有一个WCF服务库,这是一个Windows服务中通过TCP主持。 下面是什么在我的WCF服务库: 服务默认地将Impl:

[ServiceContract] 
    public interface ICalculatorService 
    { 
     [OperationContract] 
     int Add(int x, int y); 
    } 

    public class CalculatorService : ICalculatorService 
    { 
     public int Add(int x, int y) 
     { 
      return x + y; 
     } 
    } 

配置:

<system.serviceModel> 
    <services> 
     <service name="namespace.CalculatorService"> 
     <endpoint address="" binding="netTcpBinding" bindingConfiguration="" 
      contract="namespace.iCalculatorService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 
      contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/CalculatorService/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="false" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

容器配置这是WCF项目被我注册的一切城堡里(我不知道wcf设施应该在哪里注册,应该从WCF服务还是从承载WCF服务的Windows服务注册。

public class ConfigureContainerCastle 
    { 
     private readonly IWindsorContainer container; 

     public ConfigureContainerCastle(IWindsorContainer container) 
     { 
      this.container = container; 
     } 

     public void Configure() 
     { 
      // Any component registration. 
      container.Register(Component.For<ICalculatorService>().ImplementedBy<CalculatorService>()); 
      //All other component registration. 
     } 
    } 

下面是什么在我的Windows服务:

public class Host<T>:ServiceBase 
    { 
    private ServiceHost serviceHost = null; 
      protected override void OnStart(string[] args) 
      { 
       if (serviceHost != null) 
       { 
        serviceHost.Close(); 
       } 

       // How do I call the DefaultServiceHostFactory and start the service? 
       // Should I call the WCF facility from here or from WCF service library? 
//    var container = new WindsorContainer(); 
    //    var configureContainer = new DataServicesConfigureContainer(container); 
    //   var hostFactory = new DefaultServiceHostFactory(container.Kernel); 

       serviceHost = new ServiceHost(typeof (T)); 
       serviceHost.Open(); 
      } 

      protected override void OnStop() 
      { 
       if (serviceHost == null) 
        return; 

       serviceHost.Close(); 
       serviceHost = null; 
      } 
    } 

而且我对Windows服务配置(app.config)中是一样的我的WCF服务,从字面上看一行行。

现在的问题是,我如何将这一切与WCF设施连接在一起?我已经看到很多使用http和global.asax的例子,但没有一个用于windows服务。你能帮忙吗?即使是适当的链接也是有帮助的。

感谢, -Mike

回答

1

首先一个小点的 - 如果你是想TCP则需要使用net.tcp协议,像这样指定的基地址:

<add baseAddress="net.tcp://localhost:8732/CalculatorService" /> 

但是,这ISN”牛逼你的问题,所以在破碎...

(我不知道在哪里的WCF设施应进行登记,无论是应该从WCF服务或托管WCF服务的Windows服务注册)。

我建议你在接近你注册服务的地方注册设施。所以,在WCF服务库中 - 实际上你发布的代码位是一个很好的注册地点(只要它被注册了,我认为它在哪里并没有太大的区别)。

现在的问题是,我如何将这一切与WCF设施连接在一起?我已经看到很多使用http和global.asax的例子,但没有一个用于windows服务。你能帮忙吗?即使是适当的链接也是有帮助的。

倒也干脆实际上 - 如果你想保持你的配置在您的配置文件你可以改变你的Configure方法是这样的(我假设你已经有了一个参考Castle.Facilities.WcfIntegration):

public void Configure() 
{ 
    container.AddFacility<WcfFacility>(); 

    // Any component registration. 
    container.Register(Component 
     .For<ICalculatorService>() 
     .ImplementedBy<CalculatorService>() 
     .AsWcfService()); 

    //All other component registration. 
} 

就是这样 - 当WCF客户端连接到服务器时,Windsor现在负责提供服务(不要相信我 - 在CalculatorService中添加一些依赖关系,并惊叹于这个奇迹)。

您可能想要考虑彻底废除配置文件并改用Windsor fluent API--它非常直观,只需查看您可以传入AsWcfService方法的内容,然后离开(或仅仅粘贴与配置文件)。