2016-07-12 10 views
0

私はnetNamedPipeBindingを使用してホストするいくつかのサービスを持っており、これらのサービスをホストするWindowsサービスを選択しました。私はIDでエンティティを照会しようとする簡単なテストをしていますが、すべてのケースでの返信応答はnullです。Entity Frameworkを使用したWindowsサービス付きAutofac + WCF +自己ホスト

すべての私のサービスは

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, 
       InstanceContextMode = InstanceContextMode.PerCall)] 

を使用する必要があると私は他の層DIが自動的に解決されているので、私の問題は、私は容器の中に私のDbContextを注入してるかであることを考えています。 。。ここ

は、自分のコードの例である:

builder.RegisterGeneric(typeof演算(RepositoryBase <>))のような(typeof演算(IRepositoryBase <>))InstancePerDependency();

// and here I'm injecting the implemented classes with their interfaces 
// and the same goes to my business classes 

// and here is the example of how I'm setting my services 
managerBuilder.Register(x => new BusinessManager(x.Resolve<IBusinessppService>())) 
         .As<IBusinessContract>() 
         .InstancePerDependency(); 

//here is the method where I'm calling to create my service host 
private void CreateServiceHost() 
{ 
     _serviceHost = new ServiceHost(typeof(TManager), _baseUri); 
     _serviceHost.AddServiceEndpoint(typeof(TContract), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), _serviceName); 

     var serviceMetadataBehavior = _serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); 

     if (serviceMetadataBehavior == null) 
     { 
      serviceMetadataBehavior = new ServiceMetadataBehavior(); 
      serviceMetadataBehavior.HttpGetEnabled = false; 
      _serviceHost.Description.Behaviors.Add(serviceMetadataBehavior); 
     } 

     var behavior = _serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>(); 
     behavior.IncludeExceptionDetailInFaults = true; 

     _serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, 
             MetadataExchangeBindings.CreateMexNamedPipeBinding(), 
             string.Format("{0}/{1}", _serviceName, "mex")); 

     _serviceHost.Description.Behaviors.Add(new AutomapperServiceBehavior()); 
     _serviceHost.AddDependencyInjectionBehavior<TContract>(_lifetimeScope); 
     _serviceHost.Faulted += _serviceHost_Faulted; 
     _serviceHost.UnknownMessageReceived += _serviceHost_UnknownMessageReceived; 
     _serviceHost.Open(); 
} 

//and I'm injecting these guys in the constructor 
public ServicePublishBase(Uri[] baseUri, ILifetimeScope lifetimeScope) 
{ 
     _serviceName = typeof(TManager).Name; 
     _lifetimeScope = lifetimeScope; 
     _baseUri = baseUri; 
} 

そして私は、すべてのホストを作成するための工場を持っており、コードが私のためにこの

var pipeBaseUrl = new Uri[] { new Uri("net.pipe://localhost/Example/") }; 

var assemblies = Assembly.GetAssembly(GetType()); 

builder.RegisterAssemblyTypes(assemblies) 
     .Where(type => type.IsAssignableTo<IServicePublish>()) 
     .WithParameter("baseUri", pipeBaseUrl) 
     .As<IServicePublish>() 
     .SingleInstance(); 

builder.Register(x => new ServicePublishFactory(x.Resolve<IEnumerable<IServicePublish>>())) 
     .SingleInstance(); 

のように、このすべては、すべてのサービスだけで結構ですされ稼働して完璧ですが、とき私はnullを返すサービスの呼び出しを行い、私はすでにデータベース、接続文字列、エンドポイントクライアントをチェックしています。

PS:私はすでにInstancePerLifetimeScopeを使用しようとしましたが、それは私のコードの=(

より多くの例が動作しません: - 私はこのように私のDbContextを注入:

builder.RegisterType<DbContext>().InstancePerLifetimeScope() 

とIF私は戻って完全に私のDbContextを取得し、手動で解決しようとする一般的なリポジトリの 私の実装は、このようなものです:。

public virtual TEntity GetById(int id) 
{ 
     return Context.Set<TEntity>().Find(id); 
} 

私はこのコードを呼び出すときに例外がありません(例えば、Contextのヌル参照のように)、すなわちコンテキストが注入されています。私のクライアントコードの

例:

var result = client.GetById(1); 

この呼び出しの結果はnullで、= 1は、データベース内にあるidが、私はデバッグのためにテキストログを持っていると私はTEntityの名前を記録している場合名前は正しいです。 また、私はSQLプロファイラを実行しています。クライアントを呼び出すと、プロファイラに何もログインしていません。

autofacはマルチスレッドwcfシナリオでdbcontextを解決できません。多分?

+0

に感謝し、すべてが 'DbContext' _除く_正しく注入されつつあるように聞こえます。どこにオートファックを登録しますか?私はそれを上には見ません。コンテナをビルドした直後にコンテナを取得した場合は、 'DbContext'を手動で解決できますか?例外はありますか?あなたがサービスを呼び出すと "それはあなたにnullを返しています" - サービスコール自体は機能していますが、データベースが正しく返されていないか、サービスが実際に処理の呼び出しを受け取っていないことを意味しますか? –

+0

私はこのbuilder.RegisterTypeと同じように私のDbContextを注入しています。登録番号().InstancePerLifetimeScope() –

+0

質問に記載されている内容をコメントに書き換えてください。文脈で - 例えば、他の登録と一緒に。私が尋ねた他の質問と同じ答え:質問にその情報を追加する。 –

答えて

0

よく問題が解決しました! 私はdbcontextとすべてのオブジェクトを注入する方法を変更してすべての時間を無駄にした後、Windowsサービスのパーミッションを変更し、私はデータベース間の限界を説明するこの記事を作成しました。 データベースはLocalDbでした。このタイプのデータベースには、インプロセスアプリケーションを実行するにはいくつかの制限があります。私はこのデータベースを使用しているチームでこのデータベースを使用しています。私はこれをローカルと完全なSQL Serverのインスタンスをインストールすることを解決します。

http://erikej.blogspot.com.br/2011/01/comparison-of-sql-server-compact-4-and.html

ErikEJとトラヴィスILLIG

関連する問題