Problem with responses...

Aug 17, 2014 at 10:19 PM
I have built a class RequestHandler which encapsulates the service object, Handle method, and a RpcReponseHandler callback.
public class RequestHandler<T> where T :  new()
    {
        private static T _service;

        public RequestHandler()
        {
            _service = new T();
        }

        private static void HandleRpcResponse(IAsyncResult state)
        {
            var async = ((JsonRpcStateAsync)state);
            var result = async.Result;
            var writer = ((TextWriter)async.AsyncState);

            writer.WriteLine(result);
            writer.FlushAsync();
            //writer.Flush();
        }

        public void Handle(TextWriter response, string request)
        {
            var async = new JsonRpcStateAsync(HandleRpcResponse, response) { JsonRpc = request };
              //response.WriteLine("Beginning the request handle...");
              //response.Flush();
            JsonRpcProcessor.Process(async, response);
        }
    }
The RequestHandler is created in host app of socket server, and the handle method is passed to the socket listener Listen method.
The problem is that not all requests get the responses. Sometimesthe response is empty.

The following is the log from socket client:

Connecting socket to 127.0.0.1:10001
Connection established to 127.0.0.1:10001
Sending message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket write completed for message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket read got a string message:
Sending message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket write completed for message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket read got a string message: {"jsonrpc":"2.0","result":"81f78e49-2ab0-4508-807a-96671435a247","id":"1"}

Sending message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket write completed for message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket read got a string message: {"jsonrpc":"2.0","result":"398d4f39-5f55-41d9-a2f8-b20cce2140f5","id":"1"}

Sending message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket write completed for message <string message: {'method':'GetNewJobId','params':[],'id':'1'}>
Socket read got a string message:

Is there a problem with asynchronous callbacks or something?

Help please...

Bartek
Coordinator
Aug 18, 2014 at 4:27 AM
Try sending unique ID's in your request. Let me know if that changes things.

Aug 18, 2014 at 6:56 AM
Edited Aug 18, 2014 at 12:08 PM
I have been changing 'id' but the result is the same.

I have made some more tests, and found that methods in service works fine and the callback HandleRpcResponse is executed.

The problem is inside this callback method. I have logged from this method to file and the result variable is set with correct value, but when i try to write it with writer a socket log an empty string (and sends it as response).

I think the problem is in threads, but dont know how to diagnose it. I'm still learning.
I will try to pass the RequestHandler to SocketServer, not only the Handle method, and maybe it will help.

Edit:

I have debugged the windows service with socket server and figured out that when I made the call to a writer (in Callback method) I get the ObjectDisposedException.

Is the JsonRPcProcessor.Process method executed in separate thread?

Edit2:

Problem solved.
I wasn't blocking the socket thread until callback used the "writer" variable, and the socket thread disposed the "writer". After using a Task<string> Process in Handler everything is OK.
Marked as answer by AustinHarris on 8/18/2014 at 9:24 PM
Coordinator
Aug 19, 2014 at 4:24 AM
I'm glad you got it figured out. Sorry I wasn't more help!
Aug 19, 2014 at 6:51 AM
No problem :)

My knowledge about async programming is minor and that's why I had problem. Moreover I have to try figure out how to do this not using Tasks but IAsyncResult. Just for my curiosity :) .

Thanks anyway.