Permalink
Browse files

adding samples

  • Loading branch information...
1 parent 0ebd654 commit ca7fc6c2d657fb810ccd5b3634e8c35140491553 @tjanczuk committed Mar 19, 2013
@@ -0,0 +1,10 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func('async (input) => { return ".NET welcomes " + input.ToString(); }');
+
+hello('Node.js', function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
@@ -0,0 +1,15 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func(function () {/*
+ async (input) =>
+ {
+ return ".NET welcomes " + input.ToString();
+ }
+*/});
+
+hello('Node.js', function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
@@ -0,0 +1,4 @@
+async (input) =>
+{
+ return ".NET welcomes " + input.ToString();
+}
@@ -0,0 +1,10 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func('103_hello_file.csx');
+
+hello('Node.js', function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
@@ -0,0 +1,33 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var add7 = edge.func(function () {/*
+ using System.Threading.Tasks;
+
+ public class Startup
+ {
+ public async Task<object> Invoke(object input)
+ {
+ return this.Add7((int)input);
+ }
+
+ int Add7(int v)
+ {
+ return Helper.Add7(v);
+ }
+ }
+
+ static class Helper
+ {
+ public static int Add7(int v)
+ {
+ return v + 7;
+ }
+ }
+*/});
+
+add7(12, function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
@@ -0,0 +1,13 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+// Compile Sample105.dll with
+// csc.exe /target:library /debug Sample105.cs
+
+var edge = require('edge');
+
+var add7 = edge.func('Sample105.dll');
+
+add7(12, function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
@@ -0,0 +1,40 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func(function () {/*
+ using System;
+ using System.Threading.Tasks;
+ using System.Collections.Generic;
+
+ public class Startup
+ {
+ public async Task<object> Invoke(IDictionary<string,object> input)
+ {
+ Console.WriteLine("-----> In .NET:");
+ foreach (var kv in input)
+ {
+ Console.WriteLine(kv.Key + ": " + kv.Value.GetType());
+ }
+
+ return null;
+ }
+ }
+*/});
+
+var payload = {
+ anInteger: 1,
+ aNumber: 3.1415,
+ aString: 'foobar',
+ aBool: true,
+ anObject: {},
+ anArray: [ 'a', 1, true ],
+ aBuffer: new Buffer(1024)
+}
+
+console.log('-----> In node.js:');
+console.log(payload);
+
+hello(payload, function (error, result) {
+ if (error) throw error;
+});
@@ -0,0 +1,26 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func(function () {/*
+ async (input) =>
+ {
+ var result = new {
+ anInteger = 1,
+ aNumber = 3.1415,
+ aString = "foobar",
+ aBool = true,
+ anObject = new { a = "b", c = 12 },
+ anArray = new object[] { "a", 1, true },
+ aBuffer = new byte[1024]
+ };
+
+ return result;
+ }
+*/});
+
+hello(null, function (error, result) {
+ if (error) throw error;
+ console.log('-----> In node.js:');
+ console.log(result);
+});
View
@@ -0,0 +1,32 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var addAndMultiplyBy2 = edge.func(function () {/*
+ using System;
+ using System.Threading.Tasks;
+ using System.Collections.Generic;
+
+ public class Startup
+ {
+ public async Task<object> Invoke(IDictionary<string,object> input)
+ {
+ int sum = (int)(input["a"]) + (int)(input["b"]);
+ var multiplyBy2 = (Func<object,Task<object>>)input["multiplyBy2"];
+ return await multiplyBy2(sum);
+ }
+ }
+*/});
+
+var payload = {
+ a: 2,
+ b: 3,
+ multiplyBy2: function(input, callback) {
+ callback(null, input * 2);
+ }
+};
+
+addAndMultiplyBy2(payload, function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
View
@@ -0,0 +1,15 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func('async (input) => { return ".NET welcomes " + input.ToString(); }');
+
+// call the function synchronously
+var result = hello('Node.js', true);
+console.log(result);
+
+// call the same function asynchronously
+hello('JavaScript', function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
View
@@ -0,0 +1,30 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var hello = edge.func(function () {/*
+ async (input) =>
+ {
+ // we are on V8 thread here
+
+ return await Task.Run<object>(async () => {
+
+ // we are on CLR thread pool thread here
+
+ // simulate long running operation
+ await Task.Delay(5000);
+
+ return ".NET welcomes " + input.ToString();
+ });
+ }
+*/});
+
+console.log('Starting CPU bound operation...');
+hello('Node.js', function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
+
+setInterval(function() {
+ console.log('Node.js event loop is alive');
+}, 1000);
View
@@ -0,0 +1,98 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+//#r "System.dll"
+//#r "System.Data.dll"
+//#r "System.Web.Extensions.dll"
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.SqlClient;
+using System.IO;
+using System.Text;
+using System.Threading.Tasks;
+using System.Web.Script.Serialization;
+
+public class Startup
+{
+ public async Task<object> Invoke(string command)
+ {
+ string connectionString = Environment.GetEnvironmentVariable("OWIN_SQL_CONNECTION_STRING");
+
+ if (command.StartsWith("select ", StringComparison.InvariantCultureIgnoreCase))
+ {
+ return await this.ExecuteQuery(connectionString, command);
+ }
+ else if (command.StartsWith("insert ", StringComparison.InvariantCultureIgnoreCase)
+ || command.StartsWith("update ", StringComparison.InvariantCultureIgnoreCase)
+ || command.StartsWith("delete ", StringComparison.InvariantCultureIgnoreCase))
+ {
+ return await this.ExecuteNonQuery(connectionString, command);
+ }
+ else
+ {
+ throw new InvalidOperationException("Unsupported type of SQL command. Only select, insert, update, and delete are supported.");
+ }
+ }
+
+ async Task<object> ExecuteQuery(string connectionString, string commandString)
+ {
+ List<object> rows = new List<object>();
+
+ using (SqlConnection connection = new SqlConnection(connectionString))
+ {
+ using (SqlCommand command = new SqlCommand(commandString, connection))
+ {
+ await connection.OpenAsync();
+ using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection))
+ {
+ object[] fieldNames = new object[reader.FieldCount];
+ for (int i = 0; i < reader.FieldCount; i++)
+ {
+ fieldNames[i] = reader.GetName(i);
+ }
+ rows.Add(fieldNames);
+
+ IDataRecord record = (IDataRecord)reader;
+ while (await reader.ReadAsync())
+ {
+ object[] resultRecord = new object[record.FieldCount];
+ record.GetValues(resultRecord);
+ for (int i = 0; i < record.FieldCount; i++)
+ {
+ Type type = record.GetFieldType(i);
+ if (type == typeof(byte[]) || type == typeof(char[]))
+ {
+ resultRecord[i] = Convert.ToBase64String((byte[])resultRecord[i]);
+ }
+ else if (type == typeof(Guid) || type == typeof(DateTime))
+ {
+ resultRecord[i] = resultRecord[i].ToString();
+ }
+ else if (type == typeof(IDataReader))
+ {
+ resultRecord[i] = "<IDataReader>";
+ }
+ }
+
+ rows.Add(resultRecord);
+ }
+ }
+ }
+ }
+
+ return rows;
+ }
+
+ async Task<object> ExecuteNonQuery(string connectionString, string commandString)
+ {
+ using (SqlConnection connection = new SqlConnection(connectionString))
+ {
+ using (SqlCommand command = new SqlCommand(commandString, connection))
+ {
+ await connection.OpenAsync();
+ return await command.ExecuteNonQueryAsync();
+ }
+ }
+ }
+}
View
@@ -0,0 +1,10 @@
+// Overview of edge.js: http://tjanczuk.github.com/edge
+
+var edge = require('edge');
+
+var sql = edge.func('202_sql.csx');
+
+sql('select top 2 * from Products', function (error, result) {
+ if (error) throw error;
+ console.log(result);
+});
View
@@ -0,0 +1,25 @@
+using System.Threading.Tasks;
+
+namespace Sample105
+{
+ public class Startup
+ {
+ public async Task<object> Invoke(object input)
+ {
+ return this.Add7((int)input);
+ }
+
+ int Add7(int v)
+ {
+ return Helper.Add7(v);
+ }
+ }
+
+ static class Helper
+ {
+ public static int Add7(int v)
+ {
+ return v + 7;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit ca7fc6c

Please sign in to comment.