2012-04-11 4 views
1

は、私は、次のサービス契約と2つの具象実装を持って言う:設定ファイルにWCFのServiceContractの具体的な実装を注入オフに基づいて値dymamically

[OperationContract] 
public interface ISearchService { 
    public ICollection<string> Search(string text); 
} 

[SearchServiceBehaviour] 
public class SolrSearchService : ISearchService { 
    public ICollection<string> Search(string text) { 
     // Implementation... 
    } 
} 

[SearchServiceBehaviour] 
public class SqlSearchService : ISearchService { 
    public ICollection<string> Search(string text) { 
     // Implementation... 
    } 
} 

私が作成できるように、これらはServiceBehaviorに起因していますサービスながらインスタンスは、設定ファイルをオフに基づいて実行されています

public class SearchServiceBehaviour : Attribute, IServiceBehavior { 
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { 
     foreach (var item in serviceHostBase.ChannelDispatchers) { 
      var dispatcher = item as ChannelDispatcher; 

      if (dispatcher != null) { 
       dispatcher.Endpoints.ToList().ForEach(e => { 
        e.DispatchRuntime.InstanceProvider = new SearchServiceInstanceProvider(); 
       }); 
      } 
     } 
    } 
} 

public class SearchServiceInstanceProvider : IInstanceProvider { 
    public object GetInstance(InstanceContext instanceContext, Message message) { 
     // Should cache and only do this at an interval. 
     ConfigurationManager.RefreshSection("appSettings"); 
     var index = ConfigurationManager.AppSettings["UseSolr"] as string; 

     bool UseSolr; 
     bool.TryParse(index, out UseSolr); 

     if (UseSolr) 
      return new IndexedSearchService(); 
     else 
      return new SearchService(); 
    } 
} 

私の質問は、私はNinjectは、設定ファイルの変更値をオフに基づいて使用して具体的な実装を注入する方法ですか?私が次の操作を実行する必要があるように思わ:

InstanceProvider
public class SearchServiceModule : NinjectModule { 
    private bool UseSolr; 

    public SearchServiceModule() { 
     // Should cache and only do this at an interval. 
     ConfigurationManager.RefreshSection("appSettings"); 
     var index = ConfigurationManager.AppSettings["UseSolr"] as string; 

     bool.TryParse(index, out UseSolr); 
    } 

    public override Load() { 
     if (UseSolr) 
      Bind<ISearchService>().To<SolrSearchService>(); 
     else 
      Bind<ISearchService>().To<SqlSearchService>(); 
    } 
} 

そして:

public object GetInstance(InstanceContext instanceContext, Message message) { 
    return _kernel.Get<ISearchService>(); 
} 

私は、configファイルの値を変更した後、しかし、Ninjectでバインディングは変更されません。 。設定ファイルの値に基づいてバインディングを変更する方法はありますか?私はここで何か間違っていますか?フィールドと同じ名前のローカル変数があるので、あなたが期待するよう

+0

このサービスはIISでホストされているのか、コンソール/ Windowsサービスでは自分でホストされていますか? –

+0

@DrewMarsh自己Windowsサービスでホストされています。 – Soliah

答えて

4

あなたのモジュールは動作しません。

はまた、条件付きバインディングを見てみましょう。 .Whenは()

+0

エラーを修正しました。それを手で入力していた。 – Soliah

1

は、Windowsサービスにセルフホスティングしています。 .configファイルを保存しても、自動的にWindowsサービスがリサイクルされることはなく、(IISとは異なり).configの変更が通知されます。したがって、起動時にカーネルを構築している場合、カーネルを再構築させるトリガはありません。

あなたは条件付きでコンフィグ設定のオフに基づいて実装を提供する​​メソッドを使用するように実装を変更する必要があります。これは次のようになります。

public override Load() 
{ 
    Bind<ISearchService>().To<SolrSearchService>().When(r => ShouldUseSolr()); 
    Bind<ISearchService>().To<SqlSearchService>().When(r => !ShouldUseSolr()); 
} 

private static ShouldUseSolr() 
{ 
    // Should cache and only do this at an interval. 
    ConfigurationManager.RefreshSection("appSettings"); 
    var index = ConfigurationManager.AppSettings["UseSolr"] as string; 

    bool result; 

    bool.TryParse(index, out result); 

    return result; 
} 
+0

私は 'ConfigurationManager.RefreshSection(" appSettings ");'を呼び出して設定ファイルを読み直しています。 – Soliah

+0

私はあなたが今起こっていると思うことに基づいて私の答えを更新しました。 –

関連する問題