CloudCode C# Module Issues: Previously working code now throws errors

I'm having a lot of issues with the c# module that was previously working in CloudCode. I didn't change anything and it started to throw errors. For example, this is the C# on CloudCode

    [CloudCodeFunction("ModeratorCheck")]
    public async Task<string> ModeratorCheck(string _customQuestion)
    {

        try
        {
            api = new OpenAIAPI("API_KEY");
            var result = await api.Moderation.CallModerationAsync(_customQuestion);
            if (result.Results[0].Flagged)
            {
                return result.Results[0].MainContentFlag;
            }
            else
            {
                return "OK";
            }
        }catch (ApiException ex)
        {
            _logger.LogError("Failed to process moderation api");
            return "exception";
        }
    }

And this is how its called in the game. I am running this from the Editor but I also have this error on my android build.

    public async Task<string> ModerateQuestion(string _customQuestion)
    {
        try
        {
            var result = await CloudCodeService.Instance.CallModuleEndpointAsync("Project", "ModeratorCheck", new Dictionary<string, object>
                {
                    { "_customQuestion", _customQuestion }
                });

            return result;
        }
        catch (CloudCodeException exception)
        {
            Debug.Log("Moderator exception "+exception);
        }
        return "error";
    }

This is the error being thrown all the time:

Cloud Code returned an Unprocessable Entity exception, but it could not be parsed: Cloud Code Unprocessable Entity exception is in an unexpected format and couldn't be parsed: StartIndex cannot be less than zero.
Parameter name: startIndex. Original error: ScriptError
(422) HTTP/1.1 422 Unprocessable Entity
Unprocessable Entity
Invocation Error
Common.Exceptions.InvalidModuleException: Error in Cloud Code Setup

Hi @mChapuis

Do you have any code that implements the ICloudCodeSetup interface within the module?

For example:

public class ModuleConfig : ICloudCodeSetup
{
     public void Setup(ICloudCodeConfig config)
     {
         config.Dependencies.AddSingleton(AdminApiClient.Create());
     }
}

That exception is thrown when something within the ICloudCodeSetup usage has failed.

I have this one

    public class ModuleConfig : ICloudCodeSetup
    {
        public void Setup(ICloudCodeConfig config)
        {
            config.Dependencies.AddSingleton(GameApiClient.Create());
        }
    }

I will test with the Admin one

Nope. Same thing is happening

Has anything changed since November/December 2023? Is my code deprecated in any way? Should I update something?

I want to release my app soon but the service I am offering has just stopped working for no apparent reason

Hi, would you be able to share the version you're using for the Cloud Code Core and API packages? It's possible that there has been some unintended breaking change in one of those - if you update to the latest version of those packages, does that fix your error?

Hi! I updated the packages, I was on Cloud Code 2.5.1 and I updated to 2.6.2. I am using Com.Unity.Services.CloudCode.Apis 0.0.12 and Com.Unity.Services.CloudCode.Core 0.0.1 for the C#

The error is the same with:

ScriptError
(422) HTTP/1.1 422 Unprocessable Entity
Unprocessable Entity
Invocation Error
Common.Exceptions.InvalidModuleException: Error in Cloud Code Setup

Is there something I can test to fix this? Any clues?

Which version of .NET are you using? Currently we support up to 7 (next Cloud Code update will support higher versions).

Could you check what version (if any) you have installed of the following:

  • Microsoft.Extensions.DependencyInjection.Abstractions
  • Microsoft.Extensions.Logging.Abstractions

Again 7 is currently the highest supported version

1 Like

I had 8.0.1 installed for both

  • Microsoft.Extensions.DependencyInjection.Abstractions
  • Microsoft.Extensions.Logging.Abstractions

I made the change to 7 and it fixed it! Thank you so much :)

These were my publishing settings but it didn't matter I guess:

9886824--1426845--upload_2024-6-12_10-54-47.png

1 Like