diff --git a/samples/SocialWeather/IStreamFormatter.cs b/samples/SocialWeather/IStreamFormatter.cs index e2c1c8f7a6..4d6f0e63ac 100644 --- a/samples/SocialWeather/IStreamFormatter.cs +++ b/samples/SocialWeather/IStreamFormatter.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; +using System.IO; using System.Threading.Tasks; namespace SocialWeather diff --git a/samples/SocialWeather/PipeWeatherStreamFormatter.cs b/samples/SocialWeather/PipeWeatherStreamFormatter.cs new file mode 100644 index 0000000000..ee57f17404 --- /dev/null +++ b/samples/SocialWeather/PipeWeatherStreamFormatter.cs @@ -0,0 +1,63 @@ +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; + +namespace SocialWeather +{ + public class PipeWeatherStreamFormatter : IStreamFormatter + { + public async Task ReadAsync(Stream stream) + { + var sr = new StreamReader(stream); + var line = await sr.ReadLineAsync(); + + if (line == null) + { + return null; + } + + var tokens = line.Split('|'); + int temperature; + long reportTime = long.MinValue; + Weather weather = (Weather)(-1); + string zipCode = tokens.Length > 3 ? tokens[3] : string.Empty; + + if(tokens.Length == 0 || !int.TryParse(tokens[0], out temperature)) + { + temperature = int.MinValue; + } + + if (tokens.Length < 2 || !long.TryParse(tokens[1], out reportTime)) + { + temperature = int.MinValue; + } + + if (tokens.Length < 3 || !Enum.TryParse(tokens[2], out weather)) + { + weather = (Weather)(-1); + } + + return new WeatherReport + { + Temperature = temperature, + ReportTime = reportTime, + Weather = weather, + ZipCode = zipCode + }; + } + + public async Task WriteAsync(WeatherReport report, Stream stream) + { + var sw = new StreamWriter(stream); + var line = $"{report.Temperature}|{report.ReportTime}|{(int)report.Weather}|{report.ZipCode ?? string.Empty}"; + + Encoding utf8 = Encoding.UTF8; + var encodedBytes = utf8.GetBytes(line); + var convertedBytes = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, encodedBytes); + + await sw.WriteLineAsync(Encoding.ASCII.GetString(convertedBytes)); + await sw.FlushAsync(); + } + } +} diff --git a/samples/SocialWeather/Startup.cs b/samples/SocialWeather/Startup.cs index 51bd818956..6209e4580f 100644 --- a/samples/SocialWeather/Startup.cs +++ b/samples/SocialWeather/Startup.cs @@ -8,19 +8,17 @@ namespace SocialWeather { public class Startup { - // This method gets called by the runtime. Use this method to add services to the container. - // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddRouting(); services.AddSingleton(); services.AddTransient(); services.AddSingleton(typeof(JsonStreamFormatter<>), typeof(JsonStreamFormatter<>)); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); @@ -36,11 +34,7 @@ namespace SocialWeather var formatterResolver = app.ApplicationServices.GetRequiredService(); formatterResolver.AddFormatter>("json"); formatterResolver.AddFormatter("protobuf"); - - app.Run(async (context) => - { - await context.Response.WriteAsync("Hello World!"); - }); + formatterResolver.AddFormatter("pipe"); } } } diff --git a/samples/SocialWeather/web.config b/samples/SocialWeather/web.config index dc0514fca5..09b33e27da 100644 --- a/samples/SocialWeather/web.config +++ b/samples/SocialWeather/web.config @@ -9,6 +9,6 @@ - + diff --git a/samples/SocialWeather/wwwroot/weather.html b/samples/SocialWeather/wwwroot/weather.html index 5ba5ef7710..df28df3435 100644 --- a/samples/SocialWeather/wwwroot/weather.html +++ b/samples/SocialWeather/wwwroot/weather.html @@ -21,7 +21,14 @@

Weather reports

-
+ + + + + + + +
ZipCodeTemperatureWeatherUpdated on:

Status:

@@ -87,7 +94,6 @@ ReportTime: Date.now(), ZipCode: get('zipCode').value })); - event.preventDefault(); }); });