Skip to content

Commit 145e437

Browse files
authored
Merge pull request #13 from jacqueskang/bugfix-guid-conversion
support guid type
2 parents cbef527 + 772a07f commit 145e437

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ private static async Task MainAsync(string[] args)
4444
// test 5: call IPC service method with enum parameter
4545
string text = await client.InvokeAsync(x => x.ConvertText("hEllO woRd!", TextStyle.Upper));
4646
Console.WriteLine(text);
47+
48+
// test 6: call IPC service method returning GUID
49+
Guid generatedId = await client.InvokeAsync(x => x.GenerateId());
50+
Console.WriteLine($"generated ID is: {generatedId}");
4751
}
4852
catch (Exception ex)
4953
{

src/IpcServiceSample.ConsoleServer/ComputingService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using IpcServiceSample.ServiceContracts;
45
using Microsoft.Extensions.Logging;
@@ -52,5 +53,10 @@ public string ConvertText(string text, TextStyle style)
5253

5354
public void DoNothing()
5455
{ }
56+
57+
public Guid GenerateId()
58+
{
59+
return Guid.NewGuid();
60+
}
5561
}
5662
}

src/IpcServiceSample.ServiceContracts/IComputingService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace IpcServiceSample.ServiceContracts
45
{
@@ -9,6 +10,7 @@ public interface IComputingService
910
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);
1011
void DoNothing();
1112
string ConvertText(string text, TextStyle style);
13+
Guid GenerateId();
1214
}
1315

1416
public class ComplexNumber

src/JKang.IpcServiceFramework.Core.Tests/DefaultValueConverterTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ public void TryConvert_Int32ToEnum()
164164
Assert.AreEqual(expected, actual);
165165
}
166166

167+
[TestMethod]
168+
public void TryConvert_StringToGuid()
169+
{
170+
var expected = Guid.NewGuid();
171+
172+
bool succeed = _sut.TryConvert(expected.ToString(), typeof(Guid), out object actual);
173+
174+
Assert.IsTrue(succeed);
175+
Assert.IsInstanceOfType(actual, typeof(Guid));
176+
Assert.AreEqual(expected, actual);
177+
}
178+
167179
interface IComplexType
168180
{
169181
int Int32Value { get; }

src/JKang.IpcServiceFramework.Core/Services/DefaultValueConverter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public bool TryConvert(object origValue, Type destType, out object destValue)
3838
}
3939
}
4040

41+
if (origValue is string str2 && destType == typeof(Guid))
42+
{
43+
if (Guid.TryParse(str2, out Guid result))
44+
{
45+
destValue = result;
46+
return true;
47+
}
48+
}
49+
4150
if (origValue is JObject jObj)
4251
{
4352
// rely on JSON.Net to convert complexe type

0 commit comments

Comments
 (0)