vmware-archive / runtimes

Kubeless function runtimes: https://kubeless.io/docs/runtimes/
Apache License 2.0
81 stars 89 forks source link

Error "Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<object>" #67

Closed har07 closed 4 years ago

har07 commented 4 years ago

As the title said, I got error "Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<object>'" when trying a simple function using dotnetcore 3.1 runtime. Here is the function log:


info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://testkedatabase/  
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Kubeless.WebAPI.Controllers.RuntimeController.Execute (Kubeless.WebAPI)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Execute", controller = "Runtime"}. Executing controller action with signature System.Threading.Tasks.Task`1[System.Object] Execute() on controller Kubeless.WebAPI.Controllers.RuntimeController (Kubeless.WebAPI).
info: Kubeless.WebAPI.Controllers.RuntimeController[0]
      04/23/2020 03:39:37: Function Started. HTTP Method: GET, Path: /.
crit: Kubeless.WebAPI.Controllers.RuntimeController[0]
      04/23/2020 03:39:37: Function Corrupted. HTTP Response: 500. Reason: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<object>'.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<object>'
   at CallSite.Target(Closure , CallSite , Object )
   at Kubeless.Core.Invokers.CompiledFunctionInvoker.Execute(Event kubelessEvent, Context kubelessContext) in /app/Kubeless.Core/Invokers/CompiledFunctionInvoker.cs:line 49
   at Kubeless.WebAPI.Controllers.RuntimeController.Execute() in /app/Kubeless.WebAPI/Controllers/RuntimeController.cs:line 43
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ProblemDetails'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action Kubeless.WebAPI.Controllers.RuntimeController.Execute (Kubeless.WebAPI) in 7.493ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Kubeless.WebAPI.Controllers.RuntimeController.Execute (Kubeless.WebAPI)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 10.4912ms 500 application/problem+json; charset=utf-8

function:

using System;
using Kubeless.Functions;

public class module
{
    public string handler(Event k8Event, Context k8Context)
    {
        string plantID = "XXX";
        return plantID;
    }
}

csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Kubeless.Functions" Version="0.1.1" />
  </ItemGroup>
</Project>
lorenzo-ange commented 4 years ago

Hi @har07 , with the runtime update to .NET Core 3.1 all synchronous code has been migrated to async to be compliant with the new ASP.NET Core 3 directive that discourages the usage of synchronous operations (https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.0#http-synchronous-io-disabled-in-all-servers)

so your example should become (I didn't tested the code):

using System;
using Kubeless.Functions;

public class module
{
    public async Task<string> handler(Event k8Event, Context k8Context)
    {
        string plantID = "XXX";
        return plantID;
    }
}

Try this and tell me if you have any error ;)

har07 commented 4 years ago

Hi @lorenzo-ange, thanks for responding. I just tried using async Task<string> and added using System.Threading.Tasks;:

using System;
using System.Threading.Tasks;
using Kubeless.Functions;

public class module
{
    public async Task<string> handler(Event k8Event, Context k8Context)
    {
        string plantID = "XXX";
        return plantID;
    }
}

now I got a different error:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://testkedatabase/  
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Kubeless.WebAPI.Controllers.RuntimeController.Execute (Kubeless.WebAPI)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Execute", controller = "Runtime"}. Executing controller action with signature System.Threading.Tasks.Task`1[System.Object] Execute() on controller Kubeless.WebAPI.Controllers.RuntimeController (Kubeless.WebAPI).
info: Kubeless.WebAPI.Controllers.RuntimeController[0]
      04/30/2020 09:28:30: Function Started. HTTP Method: GET, Path: /.
crit: Kubeless.WebAPI.Controllers.RuntimeController[0]
      04/30/2020 09:28:30: Function Corrupted. HTTP Response: 500. Reason: Cannot implicitly convert type 'System.Threading.Tasks.Task<string>' to 'System.Threading.Tasks.Task<object>'.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'System.Threading.Tasks.Task<string>' to 'System.Threading.Tasks.Task<object>'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at Kubeless.Core.Invokers.CompiledFunctionInvoker.Execute(Event kubelessEvent, Context kubelessContext) in /app/Kubeless.Core/Invokers/CompiledFunctionInvoker.cs:line 49
   at Kubeless.WebAPI.Controllers.RuntimeController.Execute() in /app/Kubeless.WebAPI/Controllers/RuntimeController.cs:line 43
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ProblemDetails'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action Kubeless.WebAPI.Controllers.RuntimeController.Execute (Kubeless.WebAPI) in 136.4651ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Kubeless.WebAPI.Controllers.RuntimeController.Execute (Kubeless.WebAPI)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 152.9551ms 500 application/problem+json; charset=utf-8
har07 commented 4 years ago

Now it works. Following the error message, I changed the function return type from Task<string> to Task<object> :

using System;
using System.Threading.Tasks;
using Kubeless.Functions;

public class module
{
    public async Task<object> handler(Event k8Event, Context k8Context)
    {
        string plantID = "XXX";
        return plantID;
    }
}

I'm closing this issue. Thanks for your explanation @lorenzo-ange