Skip to content

Commit

Permalink
Merge pull request #20 from dynamicweb/dbe/19777-Fix-object-reference…
Browse files Browse the repository at this point in the history
…-exception

Fix null reference exceptions
  • Loading branch information
DWDBE authored May 28, 2024
2 parents e9fc216 + d07b3ae commit 178e4e0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static Dictionary<string, ProductInfo> GetProductInfos(ResponseCacheLevel
// If it is anonymous user we can not determine what external user id is sent in the ERP request
// So we can not cache to not break anonymous requests for the different users with different external ids
// Cache per one request only
if (Context.Current.Items is object)
if (Context.Current?.Items is object)
{
return GetConnectorResponsesFromItems<ProductInfo>(ProductInfosKey);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public static void ClearAllCaches()
// Clear ProductInfo cache
try
{
Context.Current.Session.Remove(ProductInfosKey);
Context.Current?.Session?.Remove(ProductInfosKey);
List<string> keysToRemove = new List<string>();
foreach (string key in Context.Current.Session.Keys)
{
Expand All @@ -140,7 +140,7 @@ public static void ClearAllCaches()
}
foreach (string key in keysToRemove)
{
Context.Current.Session.Remove(key);
Context.Current?.Session?.Remove(key);
}
}
catch
Expand All @@ -149,7 +149,7 @@ public static void ClearAllCaches()

try
{
Context.Current.Items.Remove(ProductInfosKey);
Context.Current?.Items?.Remove(ProductInfosKey);
}
catch
{
Expand All @@ -158,15 +158,15 @@ public static void ClearAllCaches()
// Clear Responses cache
try
{
Context.Current.Session.Remove(WebOrdersKey);
Context.Current?.Session?.Remove(WebOrdersKey);
}
catch
{
}

try
{
Context.Current.Items.Remove(WebOrdersKey);
Context.Current?.Items?.Remove(WebOrdersKey);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.4.0</Version>
<Version>10.4.1</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Live Integration</Title>
<Description>Live Integration</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public override void OnNotify(string notification, NotificationArgs args)
if (args != null)
{
var myArgs = (Ecommerce.Notifications.Ecommerce.Order.AfterSaveArgs)args;
if (Context.Current.Session is object || ExecutingContext.IsBackEnd() ||
if (Context.Current?.Session is object || ExecutingContext.IsBackEnd() ||
myArgs.Order == null || myArgs.Order.OrderLines.Count <= 0 ||
!myArgs.Order.Complete || myArgs.Order.IsCart ||
!string.IsNullOrEmpty(myArgs.Order.IntegrationOrderId))
Expand Down
18 changes: 11 additions & 7 deletions src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/OrderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private static XmlDocument GetResponse(Settings settings, string requestXml, Ord

Dictionary<string, XmlDocument> responsesCache = ResponseCache.GetWebOrdersConnectorResponses(GetOrderCacheLevel(settings));

if (!createOrder && responsesCache.ContainsKey(orderIdentifier))
if (!createOrder && responsesCache is object && responsesCache.ContainsKey(orderIdentifier))
{
response = responsesCache[orderIdentifier];
}
Expand All @@ -304,14 +304,17 @@ private static XmlDocument GetResponse(Settings settings, string requestXml, Ord

NotificationManager.Notify(Notifications.Order.OnAfterSendingOrderToErp, new Notifications.Order.OnAfterSendingOrderToErpArgs(order, createOrder, response, error, settings, logger));

if (responsesCache.ContainsKey(orderIdentifier))
if (responsesCache is object)
{
responsesCache.Remove(orderIdentifier);
}
if (responsesCache.ContainsKey(orderIdentifier))
{
responsesCache.Remove(orderIdentifier);
}

if (response != null && !string.IsNullOrWhiteSpace(response.InnerXml))
{
responsesCache.Add(orderIdentifier, response);
if (response != null && !string.IsNullOrWhiteSpace(response.InnerXml))
{
responsesCache.Add(orderIdentifier, response);
}
}
}
else
Expand Down Expand Up @@ -921,6 +924,7 @@ private static bool ProcessResponse(Settings settings, XmlDocument response, Ord
// old discount lines are deleted and new discounts are not saved
// So at that time in backend the order lines will look incorrect, so order needs to be saved to keep discounts: https://vimeo.com/724424362/132443e631
if (!settings.UseUnitPrices && discountOrderLines.Count > 0 &&
Context.Current?.Request?.RawUrl is object &&
Context.Current.Request.RawUrl.Contains($"/dwapi/ecommerce/carts/{order.Secret}"))
{
Services.Orders.Save(order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override bool CalculateCart(Order cart)
{
if (Global.EnableCartCommunication(settings, cart.Complete))
{
var contextCurrency = Context.Current.Request.GetString("CurrencyCode");
var contextCurrency = Context.Current?.Request?.GetString("CurrencyCode");
if (!string.IsNullOrEmpty(contextCurrency) && !string.Equals(cart.CurrencyCode, contextCurrency, System.StringComparison.OrdinalIgnoreCase))
{
Services.Orders.ForcePriceRecalculation(cart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public static ProductInfo GetProductInfo(Product product, Settings settings, Use
if (ResponseCache.IsProductInCache(productCacheLevel, productIdentifier, user, context?.Currency))
{
Dictionary<string, ProductInfo> productInfoCache = ResponseCache.GetProductInfos(productCacheLevel, user);
productInfoCache.TryGetValue(productIdentifier, out productInfo);
productInfoCache?.TryGetValue(productIdentifier, out productInfo);
}
}
return productInfo;
Expand Down

0 comments on commit 178e4e0

Please sign in to comment.