2017-05-25 1 views
0

私は上から下へasyncを使用するServiceStackサービスを持っています。以下は、すべてのレイヤがサービス定義に折りたたまれた単純化された例です。System.WebのNullReferenceException非同期ServiceStackサービスからのGetAsyncを使用したServiceStackサービスの呼び出し

public async Task<ReadItemResponse> Get(Dto.ReadItem request) 
{ 
    using (JsonServiceClient client = new JsonServiceClient("http://someserver/itemservice")) 
    { 
     ReadItemResponse response = await client.GetAsync(new ReadItem 
     { 
      ItemNumber = request.ItemNumber 
     }); 

     return new Dto.ReadItemResponse 
     { 
      Item = new Dto.Item 
      { 
       ItemNumber = response.Item.ItemNumber, 
       ItemName = response.Item.ItemName 
      } 
     }; 
    } 
} 

上記のコードでは、サービスに対して多数の同時呼び出しを行うと、System.WebのNullReferenceExceptionが1〜30秒以内に発生します。

System.Web.dll!System.Web.ThreadContext.AssociateWithCurrentThread(bool setImpersonationContext) 
System.Web.dll!System.Web.HttpApplication.OnThreadEnterPrivate(bool setImpersonationContext) 
System.Web.dll!System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter() 
System.Web.dll!System.Web.Util.SynchronizationHelper.SafeWrapCallback(System.Action action = {Method = {System.Reflection.RuntimeMethodInfo}}) 
System.Web.dll!System.Web.Util.SynchronizationHelper.QueueAsynchronous.AnonymousMethod__0(System.Threading.Tasks.Task _) 
mscorlib.dll!System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke() 
mscorlib.dll!System.Threading.Tasks.Task.Execute() 
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj) 
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) 
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot = Id = 1131, Status = Running, Method = "Void <QueueAsynchronous>b__0(System.Threading.Tasks.Task)") 
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) 
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() 
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 
[Native to Managed Transition] 

私はSystem.Net.WebRequestような何か(JSONレスポンスを取得し、desalinizeする)への非同期呼び出しでJsonServiceClientを交換した場合、私は例外を見ることはありません。

私は同じ結果でJsvServiceClientを試しました。私は成功なしでConfigureAwait(false)を使用しようとしました。私は、web.configファイルが出て呼び出していることを保証しています

<compilation targetFramework="4.5.2" debug="true" /> 
<httpRuntime targetFramework="4.5" /> 

私はGetメソッドの本体を包んいます

問題を修正しますが、大幅にサービスのパフォーマンスが低下し
await Task.Factory.StartNew(() => 

。スループットは、2〜3秒で100の同時リクエストを処理できることから、20〜40秒に近いものまで処理されます。

また、NullReferenceExceptionが発生することなく非同期データベース呼び出しを行うことができました。

これは、間違いなくServiceClientの問題のように感じます。どんな助けでも大歓迎です。ありがとう!

+2

[NullReferenceExceptionとは何ですか?それを修正するにはどうすればいいですか?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix -it) –

+0

'JsonHttpClient'に問題がない場合はなぜそれを使用していませんか?要求ごとに新しいServiceClientインスタンスを作成する必要はありません(つまり、シングルトンインスタンスを使用するだけで済みます)、それを破棄する必要はありません。 – mythz

+0

@Ðаn投稿を盲目的にタグ付けする前に投稿をお読みください。ありがとう。 – hedjos

答えて

0

JsonServiceClientの代わりにJsonHttpClientを使用すると、問題が解決されました。

関連する問題