SessionAs<AppUserSession>() throws exception when a service is called from a razor view

I have a service:

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.

Can you include the full StackTrace please.

Do you also get this Exception when accessing the Session from IRequest directly, e.g:

var currentUser = Request.SessionAs<AppUserSession>();

Response Status

Error Code NotImplementedException

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

Errors

The “Request” object is null so var currentUser = Request.SessionAs<AppUserSession>(); throws an exception.

FYI to access service you need to use ResolveService<T>() and you should be disposing services after usage so this should at least be:

using var customerService = ResolveService<CustomerService>();
var customer = customerService.Get(requestObject);

But the recommended way to call services is to use the Service Gateway:

var customer = Gateway.Send(new GetCustomer { CustomerID = 1 });

Thanks!

ResolveService<CustomerService>(); works.

I will change the code to Gateway.Send(new GetCustomer { CustomerID = 1 }); per your suggestion.

1 Like