私はこれをグーグルで検索し、C# および Web Api 2 での非同期 HTTP 要求の概念を説明するいくつかのリンクを見つけることができました。ただし、同じ動作例を取得できませんでした。
空気をきれいにするために、私の要件は次のとおりです。クライアントが API (長時間実行される処理を行う) を呼び出すと、HTTP 202 (Accepted) を応答としてほぼ即座に返し、バックグラウンドで処理を続行する必要があります。私はここまではっきりしています。以下は、同じものをどのように実装したかに関する私のサンプルコードです。私が立ち往生しているのは、この長い処理タスクがバックグラウンドで完了すると、同じクライアントへのコールバックを起動し、HTTP 200 応答を返さなければならないことです。時間のかかる処理タスクがバックグラウンドで実行されている間に、クライアントが異なる値で別の同時リクエストを発行した可能性があります。
誰でも私を正しい方向に向けることができますか?これはコードを介してのみ可能ですか、それとも IIS レベルで実装する設定があるでしょうか。あなたの時間を感謝し、これを手伝ってください。
事前にみんなに感謝します。
これまでの私のコード。
public HttpResponseMessage Execute(string plugin, string pluginType, string grid, string version)
{
try
{
var type = this.LoadPlugin(plugin, pluginType, version);
if (type != null)
{
var method = type.GetMethod("Execute");
if (method != null)
{
new Task(() =>
{
// This line will take long to execute.
var filepath = method.Invoke(Activator.CreateInstance(type), new object[1] { grid });
// After this line it must invoke a callback to the client with the response as "filepath" and HTTP status code as 200
type = null;
}).Start();
}
else
{
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
}
}
else
{
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
}
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
private Type LoadPlugin(string plugin, string pluginType, string version)
{
Assembly assembly;
Type returnValue = null;
var pluginFile = new DirectoryInfo(this._pluginPath).GetFiles("*.dll")
.Where(file => FileVersionInfo.GetVersionInfo(file.FullName).OriginalFilename.ToUpper().Contains("TRANSFORMATION." + plugin.ToUpper()))
.OrderByDescending(time => time.LastWriteTime).FirstOrDefault();
if (pluginFile != null)
{
assembly = Assembly.LoadFrom(pluginFile.FullName);
AppDomain.CurrentDomain.Load(assembly.GetName());
returnValue = assembly.GetType("Transformation.Plugins." + pluginType);
assembly = null;
}
return returnValue;
}