Skip to content

Instantly share code, notes, and snippets.

@evbruno
Last active May 4, 2020 19:58
Show Gist options
  • Save evbruno/0cabc97b382d6c411b77332479557939 to your computer and use it in GitHub Desktop.
Save evbruno/0cabc97b382d6c411b77332479557939 to your computer and use it in GitHub Desktop.
faunadb csharp hello world

FaunaDB csharp dotnet sample Solution

Create new Console App

$ dotnet new console -o faunadb-app -f netcoreapp3.1

The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on faunadb-app/faunadb-app.csproj...
  Restore completed in 124.88 ms for /Users/ed/Projects/MSTutorial/fauna-ex1/faunadb-app/faunadb-app.csproj.

Restore succeeded.

Add reference to FaunaDB Client

option 1: local source

$ dotnet add faunadb-app/faunadb-app.csproj reference ~/fauna/faunadb-csharp/FaunaDB.Client/FaunaDB.Client.csproj
Reference `..\..\..\..\fauna\faunadb-csharp\FaunaDB.Client\FaunaDB.Client.csproj` added to the project.

$ dotnet restore faunadb-app
  Restore completed in 48.37 ms for /Users/ed/fauna/faunadb-csharp/FaunaDB.Client/FaunaDB.Client.csproj.
  Restore completed in 48.37 ms for /Users/ed/Projects/MSTutorial/fauna-ex1/faunadb-app/faunadb-app.csproj.

option 2: NuGet repo

$ cd faunadb-app/
$ dotnet add package FaunaDB.Client --version 2.11.0
  Writing /var/folders/xr/jkhhvx991994zsj4k50g7tqc0000gn/T/tmpnHsOjF.tmp
info : Adding PackageReference for package 'FaunaDB.Client' into project '/Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/faunadb-app.csproj'.
info : Restoring packages for /Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/faunadb-app.csproj...
info : Package 'FaunaDB.Client' is compatible with all the specified frameworks in project '/Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/faunadb-app.csproj'.
info : PackageReference for package 'FaunaDB.Client' version '2.11.0' added to file '/Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/faunadb-app.csproj'.
info : Committing restore...
info : Generating MSBuild file /Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/obj/faunadb-app.csproj.nuget.g.props.
info : Writing assets file to disk. Path: /Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/obj/project.assets.json
log  : Restore completed in 783.25 ms for /Users/ed/Projects/MSTutorial/fauna-ex3/faunadb-app/faunadb-app.csproj.

Edit faunadb-app/Program.cs:

$ cat faunadb-app/Program.cs
using System;
using FaunaDB.Client;
using static FaunaDB.Query.Language;
using System.Threading.Tasks;

namespace FaunaApp
{
    public class MyApp
    {
        public static async Task DoQuery0(FaunaClient client)
        {
            var resp = await client.Query(Now());
            Console.WriteLine("FaunaTime now is: {0}", resp);
        }
        static void Main(string[] args)
        {
            var client = new FaunaClient("secret", endpoint: "http://localhost:8443/");
            DoQuery0(client).Wait();
        }
    }
}

Final config file:

$ cat faunadb-app/faunadb-app.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <ProjectReference Include="..\..\..\..\fauna\faunadb-csharp\FaunaDB.Client\FaunaDB.Client.csproj" />
  </ItemGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>faunadb_app</RootNamespace>
  </PropertyGroup>

</Project>

Running the solution

$ dotnet run -p faunadb-app/faunadb-app.csproj
FaunaTime now is: FaunaTime(2020-05-04T19:44:35.612Z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment