simplify running of C# RouteGuide example

pull/22930/head
Jan Tattermusch 5 years ago
parent cce83b9971
commit d92cd56424
  1. 2
      examples/csharp/RouteGuide/RouteGuide/RouteGuide.csproj
  2. 22
      examples/csharp/RouteGuide/RouteGuide/RouteGuideUtil.cs
  3. 2
      examples/csharp/RouteGuide/RouteGuideClient/Program.cs
  4. 2
      examples/csharp/RouteGuide/RouteGuideServer/Program.cs

@ -16,7 +16,7 @@
</ItemGroup>
<ItemGroup>
<None Include="route_guide_db.json" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="route_guide_db.json" />
</ItemGroup>
</Project>

@ -18,6 +18,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -28,7 +29,7 @@ namespace Routeguide
/// </summary>
public static class RouteGuideUtil
{
public const string DefaultFeaturesFile = "route_guide_db.json";
public const string DefaultFeaturesResourceName = "RouteGuide.route_guide_db.json";
private const double CoordFactor = 1e7;
@ -90,12 +91,12 @@ namespace Routeguide
}
/// <summary>
/// Parses features from a JSON file.
/// Parses features from an embedded resource.
/// </summary>
public static List<Feature> ParseFeatures(string filename)
public static List<Feature> LoadFeatures()
{
var features = new List<Feature>();
var jsonFeatures = JsonConvert.DeserializeObject<List<JsonFeature>>(File.ReadAllText(filename));
var jsonFeatures = JsonConvert.DeserializeObject<List<JsonFeature>>(ReadFeaturesFromResource());
foreach(var jsonFeature in jsonFeatures)
{
@ -108,6 +109,19 @@ namespace Routeguide
return features;
}
private static string ReadFeaturesFromResource()
{
var stream = typeof(RouteGuideUtil).GetTypeInfo().Assembly.GetManifestResourceStream(DefaultFeaturesResourceName);
if (stream == null)
{
throw new IOException(string.Format("Error loading the embedded resource \"{0}\"", DefaultFeaturesResourceName));
}
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
#pragma warning disable 0649 // Suppresses "Field 'x' is never assigned to".
private class JsonFeature
{

@ -228,7 +228,7 @@ namespace Routeguide
client.ListFeatures(400000000, -750000000, 420000000, -730000000).Wait();
// Record a few randomly selected points from the features file.
client.RecordRoute(RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile), 10).Wait();
client.RecordRoute(RouteGuideUtil.LoadFeatures(), 10).Wait();
// Send and receive some notes.
client.RouteChat().Wait();

@ -27,7 +27,7 @@ namespace Routeguide
{
const int Port = 50052;
var features = RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile);
var features = RouteGuideUtil.LoadFeatures();
Server server = new Server
{

Loading…
Cancel
Save