2015-02-09 113 views
0

我在通用WCF主​​機,其掃描插件文件夾並加載實現一種「IWebService」的所有組件註冊已存在的URI

然後我用Ninject啓動託管WCF服務工作。 所有配置都在App.config中。

當只有一個插件加載時,此功能非常有用。

但是,當我嘗試啓動()的第二,它拋出一個異常,指出「註冊已存在的URI」

我在App.config所以我得到真正地定義2個完全不同的服務困惑。

這裏是App.config中

<services> 
     <service name="GenericWebService.WebService" behaviorConfiguration="defaultBehavior"> 
     <host> 
      <baseAddresses> 
      <!-- note, choose an available port--> 
      <add baseAddress="http://localhost:8092/GenericWebService/service" /> 

      </baseAddresses> 
     </host> 
     <endpoint address="http://localhost:8092/GenericWebService/service" name="service1" 
        binding="basicHttpBinding" bindingConfiguration="soapBinding" contract="GenericWebService.ICalculator"/> 
     </service> 

     <service name="GenericWebService.WebService2" behaviorConfiguration="defaultBehavior"> 
     <host> 
      <baseAddresses> 
      <!-- note, choose an available port--> 
      <add baseAddress="http://localhost:8093/GenericWebService2/service"/> 
      </baseAddresses> 
     </host> 
     <endpoint address="http://localhost:8093/GenericWebService2/service" name="service2" 
        binding="basicHttpBinding" bindingConfiguration="soapBinding" contract="GenericWebService.ICalculator2" /> 
     </service> 
    </services> 

這裏是我正在加載它們和啓動主機

private static void StartNinjectSelfHostFromPlugin(IEnumerable<IWebService> webServices) 
     { 
      foreach (var webService in webServices) 
      { 
       var ninjectType = (new NinjectServiceSelfHostFactory()).GetType(); 
       var wcfConfiguration = typeof (NinjectWcfConfiguration) 
        .GetMethod("Create", Type.EmptyTypes) 
        .GetGenericMethodDefinition() 
        .MakeGenericMethod(new[] 
        {webService.GetType(),ninjectType }) 
        .Invoke(null, new object[]{}); 
       var x = new NinjectSelfHostBootstrapper(CreateKernel, wcfConfiguration); 
       _selfHostBootstrappers.Add(x); 
       x.Start(); 
      } 
     } 

任何建議,將不勝感激。

更新例外

System.InvalidOperationException: The ChannelDispatcher at 'http://localhost:809 
2/GenericWebService/service' with contract(s) '"ICalculator"' is unable to open 
its IChannelListener. ---> System.InvalidOperationException: A registration alre 
ady exists for URI 'http://localhost:8092/GenericWebService/service'. 
    at System.ServiceModel.Channels.UriPrefixTable`1.RegisterUri(Uri uri, HostNama 
eComparisonMode hostNameComparisonMode, TItem item) 
    at System.ServiceModel.Channels.HttpTransportManager.Register(TransportChanne 
lListener channelListener) 
    at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListene 
r channelListener) 
    at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTranspor 
tManagersCallback selectTransportManagerCallback) 
    at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan time 
out) 
    at System.ServiceModel.Channels.HttpChannelListener`1.OnOpen(TimeSpan timeout 
) 
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
    at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) 
    --- End of inner exception stack trace --- 
    at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) 
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
    at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) 
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
    at System.ServiceModel.Channels.CommunicationObject.Open() 
    at Ninject.Extensions.Wcf.SelfHost.NinjectWcfSelfHost.Start() 
    at Ninject.Web.Common.SelfHost.NinjectSelfHostBootstrapper.Start() 
    at TestApp.Program.StartNinjectSelfHostFromPlugin(IEnumerable`1 psoWebService 
s) in c:\Users\william.davis\Documents\Visual Studio 2013\Projects\Service\TestA 
pp\Program.cs:line 80 
+0

總是包括完整的異常,包括堆棧跟蹤和內部異常。你可以檢查'IEnumerable webServices'中有多少'IWebService'? – BatteryBackupUnit 2015-02-09 06:28:08

+0

列表中有2個項目,因爲我嘗試啓動兩項服務。所提到的例外是內部前例。外層是無效操作。我會很快發佈堆棧 – Wjdavis5 2015-02-09 16:14:33

+0

經過大量的重構之後,我有這個工作,但我不知道它是否正確。爲了讓它工作,我不得不在我的webservice類中創建一個新的IKernel,並讓它從NinjectModule繼承並在那裏配置綁定。爲什麼我需要多個內核來實現這一點?我應該補充說,如果我不需要使用標準的ServiceHost Ninject這個工程 – Wjdavis5 2015-02-09 20:47:24

回答

0

重構的巨量之後,我有這個工作,但我不知道這是否是「正確的」。爲了讓它工作,我不得不在我的webservice類中創建一個新的IKernel,並讓它從NinjectModule繼承並在那裏配置綁定。爲什麼我需要多個內核來實現這一點?我應該補充說,如果我不需要Ninject這個工作,通過使用標準的ServiceHost