- DataAccessor
+ Data Accessor
{{ named.dataAccessor.datasetId }}
{{ named.dataAccessor.timeout }}
diff --git a/samples/UmbracoV9/Relewise.UmbracoV9.csproj b/samples/UmbracoV9/Relewise.UmbracoV9.csproj
index 4b02781..f41c7df 100644
--- a/samples/UmbracoV9/Relewise.UmbracoV9.csproj
+++ b/samples/UmbracoV9/Relewise.UmbracoV9.csproj
@@ -9,9 +9,9 @@
-
+
-
+
diff --git a/samples/UmbracoV9/Startup.cs b/samples/UmbracoV9/Startup.cs
index 2ebe531..5262d3b 100644
--- a/samples/UmbracoV9/Startup.cs
+++ b/samples/UmbracoV9/Startup.cs
@@ -55,7 +55,7 @@ public void ConfigureServices(IServiceCollection services)
{
// This setups the needed configuration for you to be able to interact with our API.
// You need to add you own dataset id and api-key in the appsettings.json before recommendations and search works
- services.AddRelewise(options => options.ReadFromConfiguration(_config));
+ //services.AddRelewise(options => options.ReadFromConfiguration(_config));
services.AddHttpContextAccessor();
services.AddSingleton
();
diff --git a/src/Integrations.Umbraco/Infrastructure/Mvc/Middlewares/RelewiseContentMiddleware.cs b/src/Integrations.Umbraco/Infrastructure/Mvc/Middlewares/RelewiseContentMiddleware.cs
index ee5f6db..e6e5867 100644
--- a/src/Integrations.Umbraco/Infrastructure/Mvc/Middlewares/RelewiseContentMiddleware.cs
+++ b/src/Integrations.Umbraco/Infrastructure/Mvc/Middlewares/RelewiseContentMiddleware.cs
@@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
using Relewise.Client;
using Relewise.Client.DataTypes;
using Umbraco.Cms.Core;
@@ -41,20 +42,23 @@ public async Task InvokeAsync(HttpContext context)
if (content != null && EnsureContentAndIsTrackable(content))
{
- ITracker tracker = context.RequestServices.GetRequiredService();
+ ITracker? tracker = SafeGetTracker(context);
- User user = await _userLocator.GetUser();
-
- try
- {
- await tracker.TrackAsync(new ContentView(user, content.Id.ToString()));
- }
- catch (HttpRequestException ex)
+ if (tracker != null)
{
- if (ex.StatusCode == HttpStatusCode.NotFound)
- throw new InvalidOperationException($"The Dataset Id '{tracker.DatasetId}' is not known by Relewise - You can always find your available dataset id's on https://my.relewise.com", ex);
+ User user = await _userLocator.GetUser();
+
+ try
+ {
+ await tracker.TrackAsync(new ContentView(user, content.Id.ToString()));
+ }
+ catch (HttpRequestException ex)
+ {
+ if (ex.StatusCode == HttpStatusCode.NotFound)
+ throw new InvalidOperationException($"The Dataset Id '{tracker.DatasetId}' is not known by Relewise - You can always find your available dataset id's on https://my.relewise.com", ex);
- throw;
+ throw;
+ }
}
}
}
@@ -63,6 +67,22 @@ public async Task InvokeAsync(HttpContext context)
await _next.Invoke(context);
}
+ private static ITracker? SafeGetTracker(HttpContext context)
+ {
+ try
+ {
+ return context.RequestServices.GetRequiredService();
+ }
+ catch (Exception ex)
+ {
+ ILogger? logger = context.RequestServices.GetService>();
+
+ logger?.LogError(ex, $"Unable to resolve {nameof(ITracker)}-instance. {{Message}}", ex.Message);
+
+ return null;
+ }
+ }
+
private static bool IsNotInPreview(UmbracoContextReference umbracoContextReference)
{
return umbracoContextReference.UmbracoContext?.InPreviewMode == false;