master
Harald Wolff 2019-03-15 07:43:08 +01:00
parent 009a7451c0
commit 96388a8e16
2 changed files with 51 additions and 0 deletions

View File

@ -12,6 +12,8 @@ using System.Reflection;
using System.Collections.Generic;
using Newtonsoft.Json;
using ln.http.exceptions;
using ln.logging;
using Newtonsoft.Json.Linq;
namespace ln.http.resources
{
public class CallableAttribute : Attribute
@ -84,11 +86,16 @@ namespace ln.http.resources
MethodInfo methodInfo = FindMethodSignature(methodCall.MethodName, methodCall.Parameters.Length);
methodResult.Result = methodInfo.Invoke(this, methodCall.Parameters);
} catch (Exception e)
{
methodResult = new MethodResult();
methodResult.MethodName = methodCall.MethodName;
methodResult.Exception = e;
Logging.Log(LogLevel.ERROR, "JsonCallResource: method call caught exception: {0}",e);
Logging.Log(e);
}
String result = JsonConvert.SerializeObject(methodResult);
@ -100,6 +107,46 @@ namespace ln.http.resources
return httpResponse;
}
private string SerializeResult(MethodInfo methodInfo,MethodResult methodResult)
{
if (methodResult.Exception != null)
{
return JsonConvert.SerializeObject(methodResult);
}
else
{
return FlatSerializeResult(methodResult);
}
}
private string FlatSerializeResult(MethodResult methodResult)
{
JObject jMethodResult = new JObject();
jMethodResult.Add("Exception", null);
jMethodResult.Add("MethodName", methodResult.MethodName);
JObject jResult = new JObject();
jMethodResult.Add("Result", jResult);
Type type = methodResult.GetType();
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
Type fType = fieldInfo.FieldType;
if ((fType.IsValueType) || (typeof(string).Equals(fType)) || (fType.IsArray))
{
jResult.Add(fieldInfo.Name, new JValue(fieldInfo.GetValue(methodResult.Result)));
}
else
{
jResult.Add(fieldInfo.Name, new JValue(fieldInfo.GetValue(methodResult.Result).ToString()));
}
}
return jMethodResult.ToString();
}
class MethodCall
{

View File

@ -53,6 +53,10 @@
<Project>{AD0267BB-F08C-4BE1-A88D-010D49041761}</Project>
<Name>ln.templates</Name>
</ProjectReference>
<ProjectReference Include="..\ln.logging\ln.logging.csproj">
<Project>{D471A566-9FB6-41B2-A777-3C32874ECD0E}</Project>
<Name>ln.logging</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />