public class CustomerService : Service
{
public ICustomerRepository Repository { get; set; }
public Customer Get(GetCustomer request)
{
var currentUser = SessionAs<AppUserSession>();
return Repository.GetCustomer(request.CustomerID, currentUser.UserID);
}
}
A razor view:
@inherits ViewPage<MessageInfo>
@{
ViewBag.Title = "Contracts";
Layout = "~/Views/Shared/SignedInLayout.cshtml";
}
@{
var requestObject = new GetCustomer { CustomerID = 1 };
var customerService = TryResolve<CustomerService>();
var customer = customerService.Get(requestObject);
}
....
But, the line var currentUser = SessionAs<AppUserSession>(); throws the following exception “This AppHost does not support accessing the current Request via a Singleton at ServiceStack.HostContext.GetCurrentRequest()”. This started happening after we moved to SS version 6.2.
Message This AppHost does not support accessing the current Request via a Singleton
Stack Trace
[GetContracts: 2/15/23 10:18:39 AM]: [REQUEST: {}] System.NotImplementedException: This AppHost does not support accessing the current Request via a Singleton at ServiceStack.HostContext.GetCurrentRequest() in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/HostContext.cs:line 320 at ServiceStack.SessionFeature.GetOrCreateSession[T](ICacheClient cache, IRequest httpReq, IResponse httpRes) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/SessionFeature.cs:line 75 at ServiceStack.Service.SessionAsTUserSession in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Service.cs:line 146 at MyProject.ServiceInterface.CustomerService.Get(GetCustomer request) in X:\ProjectFolder\MyProject\MyProject.ServiceInterface\CustomerService.cs:line 12x at ASP.Views.Contract.__contractlist.Execute() in c:\X:\ProjectFolder\MyProject\MyProject.Web\wwwroot\Views\Contract\contractlist.cshtml:line 17x at ServiceStack.Razor.ViewPage1.WriteTo(StreamWriter writer) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Razor/ViewPage.cs:line 86 at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, Object model, IRazorView pageInstance, Func1 layout) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Razor/Managers/RazorPageResolver.cs:line 208 at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPage(IRequest httpReq, Stream outputStream, Object model, RazorPage razorPage) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Razor/Managers/RazorPageResolver.cs:line 165 at ServiceStack.Razor.Managers.RazorPageResolver.ProcessRequestAsync(IRequest httpReq, Object dto, Stream outputStream) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Razor/Managers/RazorPageResolver.cs:line 116 at ServiceStack.Formats.HtmlFormat.d__22.MoveNext() in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Formats/HtmlFormat.cs:line 82