Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created June 28, 2016 18:11
Show Gist options
  • Save ahelland/ff88efd8b54b7b7899b3962af8eeb4e3 to your computer and use it in GitHub Desktop.
Save ahelland/ff88efd8b54b7b7899b3962af8eeb4e3 to your computer and use it in GitHub Desktop.
Snippet (based on a Universal App) for remote compiling and running .Net code using a Microsoft .Net API endpoint
using Newtonsoft.Json;
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Web.Http;
using static System.Environment;
namespace RemoteCompile
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
//Set a "Hello World" type snippet as the default
txtCodeIn.Text = $"using System;{NewLine}{NewLine}public class HelloWorld{NewLine}{{{NewLine}\t" +
$"public static void Main(){NewLine}\t{{{NewLine}\t\t" +
$"Console.WriteLine(\"Hello from Contos.io!\");{NewLine}\t}}{NewLine}}}";
}
private async void btnRun_Click(object sender, RoutedEventArgs e)
{
var apiURL = "https://www.microsoft.com/net/api/code";
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(apiURL));
request.Headers.Add("Referer", "https://www.microsoft.com/net");
var codeSnippet = txtCodeIn.Text;
var codeInput = new codeRequest { language = "csharp", captureStats = false,sources = new string[1] };
codeInput.sources[0] = codeSnippet;
var content = JsonConvert.SerializeObject(codeInput);
request.Content = new HttpStringContent(content, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
client.DefaultRequestHeaders.Accept.Add(new Windows.Web.Http.Headers.HttpMediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.SendRequestAsync(request);
var responseString = response.Content.ReadAsStringAsync().GetResults();
var codeOutput = JsonConvert.DeserializeObject<codeResponse>(responseString);
txtCodeOut.Text = codeOutput.Output[0];
}
}
//Model for the request / input
public class codeRequest
{
public string language { get; set; }
public bool captureStats { get; set; }
public string[] sources { get; set; }
}
//Model for the response / output
public class codeResponse
{
public string Phase { get; set; }
public string Reason { get; set; }
public bool Succeeded { get; set; }
public string[] Output { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment