C# Hangfire-为什么fire是一个“忘记”的例外
我有一个web api控制器操作,用于导入电子商务订单。呼叫服务器要求在5秒内做出响应,否则它认为响应失败。因此,我们希望接受请求并提交一个fire和forget Hangfire作业,以便在其自身过程中单独运行,并让控制器成功地C# Hangfire-为什么fire是一个“忘记”的例外,c#,asp.net-mvc,async-await,hangfire,C#,Asp.net Mvc,Async Await,Hangfire,我有一个web api控制器操作,用于导入电子商务订单。呼叫服务器要求在5秒内做出响应,否则它认为响应失败。因此,我们希望接受请求并提交一个fire和forget Hangfire作业,以便在其自身过程中单独运行,并让控制器成功地返回新的HttpStatusCodeResult(HttpStatusCode.OK,msg)立即,无论提交作业的结果如何 问题是,在排队的Hangfire作业中运行的方法中抛出了一个异常,即它是在orderSyncAppServ.UpdateOrderFromWebh
返回新的HttpStatusCodeResult(HttpStatusCode.OK,msg)代码>立即,无论提交作业的结果如何
问题是,在排队的Hangfire作业中运行的方法中抛出了一个异常
,即它是在orderSyncAppServ.UpdateOrderFromWebhookAsync
方法中抛出的,该方法也是一个异步方法。我们可以在Hangire仪表板中看到这一点,查看作业结果并查看异常
不希望也不希望的是,调用服务器没有得到200响应,而是收到了一些错误响应。这怎么可能,或者我的期望哪里被误导了
更新:
有趣的是,当我调试代码时,控制器操作中的最终返回会在我在orderSyncAppServ.UpdateOrderFromWebhookAsync
方法中的断点之前到达……所以现在我真的很困惑调用系统如何没有得到200响应:(该系统是Shopify,而不是我自己的环境,它们不提供查看响应的方法,但我的web服务器IIS日志确实显示了500个响应代码
这是我的密码:
[HttpPost]
public async Task<ActionResult> OrderUpdated (int storefrontId)
{
string msg = "Successfully submitted update request to system.";
string webhook = "orders/updated";
try
{
var validationResult = await ValidateStorefrontWebhook(webhook, storefrontId);
if (!validationResult.WasSuccessful) return new HttpStatusCodeResult(HttpStatusCode.OK, validationResult.Message);
var orderSyncAppServ = new SyncErpWithPlacedOrdersTask();
**Hangfire.BackgroundJob.Enqueue(() => orderSyncAppServ.UpdateOrderFromWebhookAsync(validationResult.Value, storefrontId));**
}
catch (Exception e)
{
msg = $"Exception webhook: {webhook} for storefront Id: {storefrontId}. {e.Message}.";
_logger.Error(msg);
}
return new HttpStatusCodeResult(HttpStatusCode.OK, msg);
}
有趣的是,当我调试代码时
控制器操作在我在
orderSyncAppServ.UpdateOrderFromWebhookAsync方法
这是预期的行为。UpdateOrderFromWebhookAsync
由Hangfire在进程外执行,但有一定的延迟
我认为你实际上可能有两个错误:
- 第一个,
Error将值{null}转换为类型“System.Int64”…
似乎链接到validationResult.value
(预期为Int64的第一项的字段变量id
的null值)。此错误在hangfire处于处理级别时触发。这不是控制器返回的错误,控制器早就返回了
- 第二个似乎与控制器参数的反序列化有关。这是调用者正在获取的参数,这就是它未被捕获的原因(发生在try…catch块之前)。我的建议是:记录服务器收到的确切请求。在本地复制。跟踪异常,如重复的参数。简化请求,直到确定问题
很抱歉,这不是一个真实的答案,但很难在评论中详细说明。您能在问题中包括具体的错误回答吗?@TheodorZoulias是的,对不起。它现在就在那里。嗯。我不知道可能是什么问题,但可能有其他人可以帮助。@TheodorZoulias好的,谢谢您花时间查看它。是的,它是indeed 2我发现了两个独立的问题。请参阅我的独立问题,该问题与此问题的真正令人困惑的方面相隔离,系统。ArgumentException:已添加具有相同密钥的项
错误……此外,它还有一个赏金:)
System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value)
at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__0(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.<>c.<BeginExecuteCore>b__152_0(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.<>c.<BeginProcessRequest>b__20_0(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.<>c__DisplayClass285_0.<ExecuteStepImpl>b__0()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Newtonsoft.Json.JsonSerializationException
Error converting value {null} to type 'System.Int64'. Path '[0].line_items[0].variant_id', line 1, position 2584.
Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Int64'. Path '[0].line_items[0].variant_id', line 1, position 2584. ---> System.InvalidCastException: Null object cannot be converted to a value type.
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
--- End of inner exception stack trace ---
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at ShopifyStorefrontAdapter.AppServ.ShopifyStoreFrontAdapter.<ConvertJsonOrderToOrderImportDTOList>d__13.MoveNext() in C:\inetpub\wwwroot\mozzo\Main\app\StoreFrontAdapters\ShopifyStorefrontAdapter\AppServ\ShopifyStoreFrontAdapter.cs:line 297
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at OTIS.AppServ.OrderMgmt.SyncErpWithPlacedOrdersTask.<UpdateOrderFromWebhook>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()