Register simple POCO as a service
xmedeko opened this issue · 6 comments
I would like to register a simple POCO with methods marked by [JsonRpcMethod]. I.e. I would like to have JsonRpcService.buildService as a public (static) method. May be in the 'Handler' class or some other class?
BTW. why the buildService call public method Handler.Register() by reflection?
I think you can get the effect you desire this way. Correct me if I'm wrong..
`[Test ()]
public void TestCanCreateAndRemoveSession()
{
var h = JsonRpc.Handler.GetSessionHandler ("this one");
h.Register ("workie", new Func<string,string>(x => "workie ... " + x));
var metadata = new System.Collections.Generic.List<Tuple<string,Type>> {
Tuple.Create ("sooper", typeof(string)),
Tuple.Create ("returns", typeof(string))
}.ToDictionary(x=> x.Item1, x=> x.Item2);
h.MetaData.AddService("workie", metadata, new System.Collections.Generic.Dictionary<string, object>());
string request = @"{method:'workie',params:{'sooper':'good'},id:1}";
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"workie ... good\",\"id\":1}";
string expectedResultAfterDestroy = "{\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Method not found\",\"code\":-32601,\"data\":\"The method does not exist / is not available.\"},\"id\":1}";
var result = JsonRpcProcessor.Process("this one", request);
result.Wait();
var actual1 = JObject.Parse (result.Result);
var expected1 = JObject.Parse (expectedResult);
Assert.AreEqual(expected1, actual1);
h.Destroy ();
var result2 = JsonRpcProcessor.Process("this one", request);
result2.Wait();
Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result));
}
`
Yes, this way it's possible. But I think if the JsonRpcService.buildService would be a public static method, then it could be possible to do something like BuildService(myPoco) and that's it.
Please pull latest, and test this out and give me some feedback.
https://github.com/Astn/JSON-RPC.NET/blob/master/Json-Rpc/ServiceBinder.cs
I wrote a simple test here:
https://github.com/Astn/JSON-RPC.NET/blob/master/AustinHarris.JsonRpcTestN/Test.cs # ln 43
[Test()] public void TestCanCreateMultipleServicesOfSameTypeInTheirOwnSessions()
Feel free to create a pull request with your enhancements.
Hi, I've looked already at the solution. It's OK. I have some comments to the ServiceBinder.bindService() - I had no time to meld them into a PR so maybe we may discuss them first:
- Why the
Handler.Register()is called by the reflection and nothandlerSession.Register(methodName, newDel);? - Public methods should have the first letter a capital, to the method name should be
BindService. - I suggest to change the second parameter from
serviceFactoryto simplyobject instance, so the method head would be:
public static void BindService<T>(string sessionID, object instance) { ... }
// and also with DefaultSessionId
public static void BindService<T>(object instance) { ... }I would let the service object creation the the user - e.g. it may be created by a container.
I have pulled all of your recommendations into the latest published artifact. Thanks for the thoughtful insights! They are much appreciated.