You can use HTTP handlers to process HTTP requests in an efficient manner. When you invoke a function, Function Compute runs the handler that you specify to process HTTP requests. This topic describes the structure and characteristics of HTTP handlers for C#.

Signatures for HTTP handlers

public virtual async Task<HttpResponse> HandleRequest(HttpRequest request, HttpResponse response, IFcContext fcContext)
{
}

If you use C# to write HTTP functions in Function Compute, you must import Aliyun.Serverless.Core and Aliyun.Serverless.Core.Http packages by using NuGet. Example:

  <ItemGroup>
        <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
        <PackageReference Include="Aliyun.Serverless.Core.Http" Version="1.0.3" />
  </ItemGroup>

HTTP handler example

The following example provides the sample code of a simple C# HTTP handler:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Aliyun.Serverless.Core;
using Aliyun.Serverless.Core.Http;

namespace Example
{
    public class HttpHandler : FcHttpEntrypoint
    {
        public override async Task<HttpResponse> HandleRequest(HttpRequest request, HttpResponse response,
            IFcContext fcContext)
        {
            response.StatusCode = 200;
            response.ContentType = "text/plain";
            await response.WriteAsync("hello world\n");
            return response;
        }

        protected override void Init(IWebHostBuilder builder) { }

        static void Main(string[] args) { }
    }
}
In the input parameters of the preceding example:
  • HttpRequest request: specifies the HttpRequest class.
  • HttpResponse response: specifies the HttpResponse class.
  • IFcContext fcContext: specifies the context object, including the information about the function and requests.
Note The C# HTTP handler must inherit the FcHttpEntrypoint in Aliyun.Serverless.Core.Http, where the Init function and HandleRequest must be rewritten.

Sample programs

The Function Compute libraries contain sample programs that use various handler types and interfaces. Each sample program contains methods for easy compilation and deployment. Sample programs:

dotnet3-blank-http :HTTP callback.