Berries encased in ice.

C# 9 Example

This is a minimal complete example in C# 9.0.

Example

The following example is a minimal, but nevertheless. complete example that makes use of C# 9's top-level code feature to do away with the clutter of namespace, class and main function definitions.

It:

  • Reads its configuration from the default, standard configuration file,
  • creates a minimal SN60 SimTrans transaction,
  • sends it to the SimTransIF server and
  • outputs the result to the console.

using System;
using System.IO;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using SimTransIF.Client;
using SimTransIF.Common;

//  Get configuration data

string programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string appDataPath = Path.Join(programDataPath, @"SAWaves\SimTransIF");
var config = new ConfigurationBuilder()
    .SetBasePath(appDataPath)
    .AddJsonFile("config.json", optional: true, reloadOnChange: false)
    .Build();
string uri = config.GetValue("ClientSettings:Uri");
Guid sharedId = config.GetValue("ClientSettings:SharedId");

//  Create a web client

WebClient client = new WebClient(sharedId, uri);

//  Create a minimal SN60 SimTrans transaction

string jsonString;
Sn60 data = new Sn60();
data.Token = client.Token;
data.TransactionId = "trx001";
data.District = 1;
data.Material = "MS";
data.Thickness = 5;
data.Cost = 10.0;
jsonString = JsonConvert.SerializeObject(data);

if (client.Send("SN60", jsonString))
{
    Console.WriteLine(client.Result);
    Console.WriteLine("Success");
}
else
    Console.WriteLine(client.Status.ToString());