Coder Social home page Coder Social logo

csharp-httprequest-template's Introduction

OpenFaaS C# HTTP Template

This repository contains the template for OpenFaaS using the upgraded of-watchdog which allows for higher throughput.

$ faas template pull https://github.com/distantcam/csharp-httprequest-template
$ faas new --list
Languages available as templates:
- csharp-httprequest

This template uses a middleware handler in an ASPNET Core Web API. This allows additional context available in the request (by providing the full body to the handler) and more control over the response by passing it back to the HTTP reponse context.

Using the template

First, pull the template with the faas CLI and create a new function:

$ faas-cli template pull https://github.com/distantcam/csharp-httprequest-template
$ faas-cli new --lang csharp-httprequest <function name>

In the directory that was created, using the name of you function, you'll find FunctionHandler.cs. It will look like this:

using Microsoft.AspNetCore.Http;
using System.IO;
using System.Threading.Tasks;

namespace Function
{
    public class FunctionHandler
    {
        public async Task<(int, string)> Handle(HttpRequest request)
        {
            var reader = new StreamReader(request.Body);
            var input = await reader.ReadToEndAsync();

            return (200, $"Hello! Your input was {input}");
        }
    }
}

This is a simple implementation of a hello-world function.

You are able to add packages to your function using the dotnet add package syntax. The packages will be added to your final function's container automatically.

For example, you could add the popular Newtonsoft.JSON package for formatting JSON objects.

using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Function
{
    public class SampleResponse
    {
        public string FunctionStatus { get; set; }
    }
    
    public class FunctionHandler
    {
        public Task<(int, string)> Handle(HttpRequest request)
        {
            var res = new SampleResponse();
            res.FunctionStatus = "Success";

            var output = JsonConvert.SerializeObject(res);

            return Task.FromResult((200, output));
        }
    }
}

csharp-httprequest-template's People

Contributors

distantcam avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.