When you use C# for programming in Function Compute, you must define a C# function as the handler function. After you configure an HTTP trigger for a function, the function can directly process HTTP requests, which facilitates the creation of web applications.
Background information
If you use C# to write HTTP functions in Function Compute, you must import Aliyun.Serverless.Core
and Aliyun.Serverless.Core.Http
packages through NuGet.
The following example shows the definition of a simple C# HTTP function:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Aliyun.Serverless.Core;
using Aliyun.Serverless.Core.Http;
namespace MySpace.TestHandlers
{
public class SingleHttpHandler : FcHttpEntrypoint
{
protected override void Init(IWebHostBuilder builder)
{ }
public override async Task<HttpResponse> HandleRequest(HttpRequest request, HttpResponse response, IFcContext fcContext)
{
response.StatusCode = 200;
response.ContentType = "text/plain";
await response.WriteAsync("hello world");
return response;
}
}
}
Input parameters of the function are as follows:
-
IFcContext. This parameter is the same as the IFcContext parameter of event functions.
FcHttpEntrypoint
from Aliyun.Serverless.Core.Http
. You must rewrite the Init
function and can determine whether to rewrite HandleRequest
, which is the handler
function of the function.
-
For
single functions
, you must rewriteHandleRequest
.HandleRequest
allows you to write the function according to your business logic. -
For
ASP.NET Core applications
, you must rewrite theInit
function only.
Limits
-
Request limits
If the function handler request of an HTTP trigger exceeds any of the following limits, the system returns status code
400
and error codeInvalidArgument
.Field Limit HTTP status code Error code headers The total size of the keys and values in the request header cannot exceed 4 KB. 400 InvalidArgument
path The total size of the path and query parameters cannot exceed 4 KB. body The body of the HTTP request cannot exceed 6 MB. -
Response limits
If the response exceeds any of the following limits, the system returns status code
502
and error codeBadResponse
.Field Limit HTTP status code Error code headers The total size of the keys and values in the headers cannot exceed 4 KB. 502 BadResponse
body The body of the HTTP response cannot exceed 6 MB.
Example of HTTP functions
- Example of a single function
The following example shows how to use
HttpRequest
andHttpResponse
in an HTTP function.using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Aliyun.Serverless.Core; using Aliyun.Serverless.Core.Http; using Microsoft.Extensions.Logging; namespace MySpace.TestHandlers { public class SingleHttpHandler : FcHttpEntrypoint { protected override void Init(IWebHostBuilder builder) { } public override async Task<HttpResponse> HandleRequest(HttpRequest request, HttpResponse response, IFcContext fcContext) { string method = request.Method; string relativePath = request.Path.Value; fcContext.Logger.LogInformation("method = {0}; requestPath = {1}", method, relativePath); StreamReader sr = new StreamReader(request.Body); string requestBody = sr.ReadToEnd(); fcContext.Logger.LogInformation("requestBody = {}", requestBody); // process request.Headers response.StatusCode = 200; response.Headers["Content-Type"]="text/plain"; response.Headers.Add("customheader", "v1"); await response.WriteAsync("hello world"); return response; } } }
Note If you need to use a single function, see . Create a console project andFcRemoteEntrypoint.cs
and change the code to the sample code of a single function. - Example of an ASP.NET Core application
using System; using Aliyun.Serverless.Core.Http; using Microsoft.AspNetCore.Hosting; namespace MySpace.TestWebApi { public class FcRemoteEntrypoint : FcHttpEntrypoint { protected override void Init(IWebHostBuilder builder) { builder .UseStartup<Startup>(); } } }
Procedure
More information
For more information about how to use an HTTP trigger, see Overview.