ScriptContext has not been initialized. Call 'Init()' to initialize Script Context

I am trying to use #Script to validate a create request

    [ValidateRequest( 
        Condition = "(it.CompanyId.HasValue && it.CompanyId != Guid.Empty) || (it.ResellerId.HasValue && it.ResellerId != Guid.Empty)",
        Message = "At least 1 CompanyId or ResellerId must be supplied"
    )]
    public class CreateIntegration : ICreateDb<Integration>, IReturn<IdResponse>
    {
        public Guid? CompanyId { get; set; }
        public Guid? ResellerId { get; set; }
        //.....

When I try to make the request I get error:

{
    "responseStatus": {
        "errorCode": "ScriptException",
        "message": "ScriptContext has not been initialized. Call 'Init()' to initialize Script Context.",
        "stackTrace": "ServiceStack.Script.ScriptException: ScriptContext has not been initialized. Call 'Init()' to initialize Script Context.\r\n ---> System.NotSupportedException: ScriptContext has not been initialized. Call 'Init()' to initialize Script Context.\r\n   at ServiceStack.Script.PageResult.Init() in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/PageResult.cs:line 436\r\n   at ServiceStack.Script.PageResult.WriteToAsyncInternal(Stream outputStream, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/PageResult.cs:line 281\r\n   at ServiceStack.Script.PageResult.WriteToAsync(Stream responseStream, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/PageResult.cs:line 244\r\n   at ServiceStack.Script.ScriptContextUtils.EvaluateResultAsync(PageResult pageResult) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/ScriptContext.cs:line 673\r\n   --- End of inner exception stack trace ---\r\n   at ServiceStack.Script.ScriptContextUtils.EvaluateResultAsync(PageResult pageResult) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/ScriptContext.cs:line 681\r\n   at ServiceStack.ServiceStackHost.EvalScriptAsync(PageResult pageResult, IRequest req, Dictionary`2 args) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/ServiceStackHost.cs:line 928\r\n   at ServiceStack.ScriptValidator.IsValidAsync(Object dto, IRequest request) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/TypeValidators.cs:line 235\r\n   at ServiceStack.TypeValidator.ThrowIfNotValidAsync(Object dto, IRequest request) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/TypeValidators.cs:line 312\r\n   at ServiceStack.Validators.AssertTypeValidatorsAsync(IRequest req, Object requestDto, Type requestType) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Validators.cs:line 72\r\n   at ServiceStack.Validation.ValidationFilters.RequestFilterAsync(IRequest req, IResponse res, Object requestDto, Boolean treatInfoAndWarningsAsErrors) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Validation/ValidationFilters.cs:line 27\r\n   at ServiceStack.Validation.ValidationFilters.RequestFilterAsync(IRequest req, IResponse res, Object requestDto) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Validation/ValidationFilters.cs:line 15\r\n   at ServiceStack.ServiceStackHost.ApplyRequestFiltersSingleAsync(IRequest req, IResponse res, Object requestDto) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/ServiceStackHost.Runtime.cs:line 216\r\n   at ServiceStack.ServiceStackHost.ApplyRequestFiltersAsync(IRequest req, IResponse res, Object requestDto) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/ServiceStackHost.Runtime.cs:line 144\r\n   at ServiceStack.Host.Handlers.GenericHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Host/Handlers/GenericHandler.cs:line 82\r\n\r\nSystem.NotSupportedException: ScriptContext has not been initialized. Call 'Init()' to initialize Script Context.\r\n   at ServiceStack.Script.PageResult.Init() in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/PageResult.cs:line 436\r\n   at ServiceStack.Script.PageResult.WriteToAsyncInternal(Stream outputStream, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/PageResult.cs:line 281\r\n   at ServiceStack.Script.PageResult.WriteToAsync(Stream responseStream, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/PageResult.cs:line 244\r\n   at ServiceStack.Script.ScriptContextUtils.EvaluateResultAsync(PageResult pageResult) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Common/Script/ScriptContext.cs:line 673\r\n",
        "errors": [],
        "meta": {
            "InnerMessages": "ScriptContext has not been initialized. Call 'Init()' to initialize Script Context."
        }
    }
}

I tried playing around with condition as it might not be valid #script but whatever I try I get the same error.

I do have some custom validators registered in apphost like:

ScriptContext.ScriptMethods.Add(new CogitoCustomScripts());

I have tried adding ScriptContext.Init() in Configure but I keep getting same error.

Where am I going wrong?

Can’t tell what the issue is from just this description, I’m assuming there’s an issue with the App Configuration. If you can submit a stand-alone repro on GitHub I can take a look.

The culprit was this line in Program.cs:

services.AddServiceStack(typeof(MyServices).Assembly);

When commented out the error goes away.

I guess this re-registers something without calling Init(). Is this intended behaviour?

When I remove this line my tests that use WebApplicationFactory<Program> stop working.

I made standalone repo:

Yeah when using Endpoint Routing the IOC needs to pre-configured before the Services are registered.

Try moving your configuration logic to the configuration lambda:

services.AddServiceStack(typeof(MyServices).Assembly, config => {
    config.ScriptContext.ScriptMethods.Add(new CogitoCustomScripts());
    config.ScriptContext.Init();
});

BTW I’d recommend against using ServiceStack Auth with Endpoint Routing, which is designed to support ASP .NET Core features like IdentityAuth.

All your plugins and dependencies would need to configured in ConfigureServices, i.e. before the AppHost is initialized.

Which template did you use to create this project? The ServiceStack.Auth templates are listed on the ServiceStack Auth Start Page which don’t use Endpoint Routing.

1 Like

Thanks that fixed it!

In my actual project I an using Identity, I created a nextjs project then copied the program.cs from the MVC auth template and updated the auth config.