@@ -284,16 +284,16 @@ methods and async.
284284using Fnproject.Fn.Fdk;
285285
286286using System.Runtime.CompilerServices;
287- [assembly:InternalsVisibleTo(" Function.Tests" )]
287+ [assembly:InternalsVisibleTo(" Function.Tests" )]
288288namespace Function {
289- class Greeter {
290- public string greet(string input) {
291- return string.Format(" Hello {0}!" ,
292- input.Length == 0 ? " World" : input.Trim ());
293- }
294-
295- static void Main(string[] args) { Fdk.Handle(args[0]); }
296- }
289+ class Greeter {
290+ public string greet(string input) {
291+ return string.Format(" Hello {0}!" ,
292+ input.Length == 0 ? " World" : input.Trim ());
293+ }
294+
295+ static void Main(string[] args) { Fdk.Handle(args[0]); }
296+ }
297297}
298298` ` `
299299
@@ -313,21 +313,21 @@ using Function;
313313using NUnit.Framework;
314314
315315namespace Function.Tests {
316- public class GreeterTest {
317- [Test]
318- public void TestGreetValid () {
319- Greeter greeter = new Greeter ();
320- string response = greeter.greet(" Dotnet" );
321- Assert.AreEqual(" Hello Dotnet!" , response);
322- }
323-
324- [Test]
325- public void TestGreetEmpty () {
326- Greeter greeter = new Greeter ();
327- string response = greeter.greet(" " );
328- Assert.AreEqual(" Hello World!" , response);
329- }
330- }
316+ public class GreeterTest {
317+ [Test]
318+ public void TestGreetValid () {
319+ Greeter greeter = new Greeter ();
320+ string response = greeter.greet(" Dotnet" );
321+ Assert.AreEqual(" Hello Dotnet!" , response);
322+ }
323+
324+ [Test]
325+ public void TestGreetEmpty () {
326+ Greeter greeter = new Greeter ();
327+ string response = greeter.greet(" " );
328+ Assert.AreEqual(" Hello World!" , response);
329+ }
330+ }
331331}
332332` ` `
333333
@@ -343,33 +343,42 @@ Building image fndemouser/dotnetfn:0.0.2 .......
343343Function fndemouser/dotnetfn:0.0.2 built successfully.
344344` ` `
345345
346- # # Accepting JSON Input
346+ # # Working with JSON
347347
348348Let' s convert this function to use JSON for its input and output.
349- Replace the definition of `HelloFunction ` with the following:
349+ Replace the content of `Program.cs ` with the following:
350350
351351```csharp
352352using Fnproject.Fn.Fdk;
353353using System;
354354
355355using System.Runtime.CompilerServices;
356356[assembly:InternalsVisibleTo("Function.Tests")] namespace Function {
357- class Input {
358- public string name {
359- get;
360- set;
361- }
357+ class Input {
358+ public string name {
359+ get;
360+ set;
362361 }
362+ }
363363
364- class Greeter {
365- public string greet(Input input) {
366- return string.Format("Hello {0}!", string.IsNullOrEmpty(input.name)
367- ? "World"
368- : input.name.Trim());
369- }
364+ class Output {
365+ public string message {
366+ get;
367+ set;
368+ }
370369
371- static void Main(string[] args) { Fdk.Handle(args[0]); }
370+ public Output(string message) { this.message = message; }
371+ }
372+
373+ class Greeter {
374+ public Output greet(Input input) {
375+ return new Output(string.Format(
376+ "Hello {0}!",
377+ string.IsNullOrEmpty(input.name) ? "World" : input.name.Trim()));
372378 }
379+
380+ static void Main(string[] args) { Fdk.Handle(args[0]); }
381+ }
373382}
374383```
375384
@@ -380,24 +389,24 @@ using Function;
380389using NUnit.Framework;
381390
382391namespace Function.Tests {
383- public class GreeterTest {
384- [Test]
385- public void TestGreetEmpty() {
386- Greeter greeter = new Greeter();
387- Input input = new Input();
388- string response = greeter.greet(input);
389- Assert.AreEqual("Hello World!", response);
390- }
391-
392- [Test]
393- public void TestGreetValid() {
394- Greeter greeter = new Greeter();
395- Input input = new Input();
396- input.name = "Dotnet";
397- string response = greeter.greet(input);
398- Assert.AreEqual("Hello Dotnet!", response);
399- }
392+ public class GreeterTest {
393+ [Test]
394+ public void TestGreetEmpty() {
395+ Greeter greeter = new Greeter();
396+ Input input = new Input();
397+ Output output = greeter.greet(input);
398+ Assert.AreEqual("Hello World!", output.message);
400399 }
400+
401+ [Test]
402+ public void TestGreetValid() {
403+ Greeter greeter = new Greeter();
404+ Input input = new Input();
405+ input.name = "Dotnet";
406+ Output output = greeter.greet(input);
407+ Assert.AreEqual("Hello Dotnet!", output.message);
408+ }
409+ }
401410}
402411```
403412
@@ -454,13 +463,13 @@ Use `curl` to invoke the function:
454463
455464
456465>```sh
457- > curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01G1ZZMSA7NG8G00GZJ0000002
466+ > curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01G1ZZMSA7NG8G00GZJ0000002 --data-raw ' { " name " : " Dotnet " } '
458467>```
459468
460469The result is now in a JSON format.
461470
462471```
463- Hello World!
472+ {"message":" Hello Dotnet!"}
464473```
465474
466475## Wrap Up
0 commit comments