Error when saving to database on saving/publishing a node
Hi, I'm trying to create a custom Audit Trail when saving one of my custom Document Types, the record in the database is created but I am greeted with an error within the BackOffice when it's saved, the node doesn't seem to be created/saved and there's no record of it anywhere. Please find my code snippets below.
case Message.ModelTypeAlias:
{
if (string.IsNullOrEmpty(node.GetValue<string>("dateCreated")))
{
node.SetValue("dateCreated", DateTime.Now);
node.SetValue("isRead", false);
var memberID = node.GetValue("member").ToString();
var guid = Guid.ParseExact(memberID.Substring(memberID.Length - 32), "N");
var memberService = Current.Services.MemberService;
var member = memberService.GetByKey(guid);
using (_umbracoContext.EnsureUmbracoContext())
{
if (member != null)
_auditService.Insert(Core.Enums.AuditEnum.AuditType.MessageCreated, member.Id, null);
else
_auditService.Insert(Core.Enums.AuditEnum.AuditType.MessageCreated, null, null);
}
}
break;
}
Above shows the switch statement in the ContentService_Saving override method.
Below shows my Insert code:
public bool Insert(AuditType type, int? memberID = null, int? contentID = null){
Dictionary<string, string> data = new Dictionary<string, string>();
using (var scope = _scopeProvider.CreateScope())
{
try
{
var uHelper = Umbraco.Web.Composing.Current.UmbracoHelper;
var member = memberID != null ? _services.MemberService.GetById((int)memberID) : null;
var content = contentID != null ? uHelper.Content((int)contentID) : null;
var db = scope.Database;
var contentName = content != null ? content.Name : "";
Audit toAdd = new Audit();
toAdd.ContentID = contentID;
toAdd.MemberID = memberID;
toAdd.MemberName = member != null ? member.Name : "";
toAdd.ContentName = contentName;
toAdd.TimeStamp = DateTime.Now;
toAdd.Type = type;
db.Insert<Audit>(toAdd);
db.CompleteTransaction();
scope.Complete();
return true;
}
catch(Exception ex)
{
_logger.Error(typeof(AuditService), ex);
scope.Complete();
return false;
}
}
Lastly, please find my stack trace here:
{
Exception
System.NullReferenceException: Object reference not set to an instance of an object.
at Umbraco.Core.Scoping.Scope.ReadLock(Int32[] lockIds) in D:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 495
at Umbraco.Core.Services.Implement.ContentService.GetById(Int32 id) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 348
at Umbraco.Core.Services.Implement.ContentService.GetParent(IContent content) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 648
at Umbraco.Core.Services.Implement.ContentService.StrategyCanPublish(IScope scope, IContent content, Boolean checkPath, IReadOnlyList1 culturesPublishing, IReadOnlyCollection1 culturesUnpublishing, EventMessages evtMsgs, ContentSavingEventArgs savingEventArgs, IReadOnlyCollection1 allLangs) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 2680
at Umbraco.Core.Services.Implement.ContentService.CommitDocumentChangesInternal(IScope scope, IContent content, ContentSavingEventArgs saveEventArgs, IReadOnlyCollection1 allLangs, Int32 userId, Boolean raiseEvents, Boolean branchOne, Boolean branchRoot) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 1149
at Umbraco.Core.Services.Implement.ContentService.SaveAndPublish(IContent content, String culture, Int32 userId, Boolean raiseEvents) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 905
at Umbraco.Web.Editors.ContentController.PublishInternal(ContentItemSave contentItem, String defaultCulture, String cultureForInvariantErrors, Boolean& wasCancelled, String[]& successfulCultures) in D:\a\1\s\src\Umbraco.Web\Editors\ContentController.cs:line 1217
at Umbraco.Web.Editors.ContentController.PostSaveInternal(ContentItemSave contentItem, Func2 saveMethod, Func2 mapToDisplay) in D:\a\1\s\src\Umbraco.Web\Editors\ContentController.cs:line 728
at Umbraco.Web.Editors.ContentController.PostSave(ContentItemSave contentItem) in D:\a\1\s\src\Umbraco.Web\Editors\ContentController.cs:line 599
at lambdamethod(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>cDisplayClass62.
Error when saving to database on saving/publishing a node
Hi, I'm trying to create a custom Audit Trail when saving one of my custom Document Types, the record in the database is created but I am greeted with an error within the BackOffice when it's saved, the node doesn't seem to be created/saved and there's no record of it anywhere. Please find my code snippets below.
Above shows the switch statement in the
ContentService_Saving
override method.Below shows my Insert code:
Lastly, please find my stack trace here:
Properties Timestamp 2020-04-20T11:21:01.8427587+00:00 @MessageTemplate Unhandled controller exception occurred for request '{RequestUrl}' RequestUrl http://localhost:54237/umbraco/backoffice/UmbracoApi/Content/PostSave SourceContext Umbraco.Web.Editors.ContentController ProcessId 896 ProcessName iisexpress ThreadId 10 AppDomainId 2 AppDomainAppId LMW3SVC3ROOT MachineName DESKTOP-JIN7H3L Log4NetLevel ERROR HttpRequestNumber 4 HttpRequestId ed5b120a-ab3a-4308-8252-ce8d6695a659 }
is working on a reply...