Search code examples
c#c++-cliprotocol-buffersrpc

timeout while making RPC Call form C# to C++/CLI


public ref class RequestMessage
{
public:
    [ProtoMember(1)]
    Int32 Number1;
    [ProtoMember(2)]
    Int32 Number2;
};

// Response message declared as managed class.
public ref class ResponseMessage
{
    public:
    [ProtoMember(1)]
    Int32 Result;
};

void OnMessageReceived(System::Object ^sender, TypedRequestReceivedEventArgs<RequestMessage^> ^e);
int main(array<System::String ^> ^args)
{
    ISerializer ^aSerializer = gcnew ProtoBufSerializer();
    IDuplexTypedMessagesFactory ^aReceiverFactory = gcnew DuplexTypedMessagesFactory(aSerializer);
    IDuplexTypedMessageReceiver<ResponseMessage^, RequestMessage^> ^aReceiver =aReceiverFactory->CreateDuplexTypedMessageReceiver<ResponseMessage^, RequestMessage^>();
    aReceiver->MessageReceived +=    gcnew System::EventHandler<Eneter::Messaging::EndPoints::TypedMessages::TypedRequestReceivedEventArgs<RequestMessage ^> ^>(&OnMessageReceived);
    IMessagingSystemFactory ^aMessaging = gcnew TcpMessagingSystemFactory();
    IDuplexInputChannel ^anInputChannel =aMessaging->CreateDuplexInputChannel("tcp://127.0.0.1:4502/");
    aReceiver->AttachDuplexInputChannel(anInputChannel);
    Console::WriteLine("The calculator service is running. Press ENTER to stop.");
    Console::ReadLine();
    aReceiver->DetachDuplexInputChannel();

    return 0;
}

void OnMessageReceived(System::Object ^sender,TypedRequestReceivedEventArgs<RequestMessage ^> ^e)
{
    ResponseMessage ^aResponseMessage = gcnew ResponseMessage();
    aResponseMessage->Result = e->RequestMessage->Number1 + e->RequestMessage->Number2;

    Console::WriteLine("{0} + {1} = {2}", e->RequestMessage->Number1, e->RequestMessage->Number2, aResponseMessage->Result);

    // Send back the response message.
    IDuplexTypedMessageReceiver<ResponseMessage^, RequestMessage^> ^aReceiver = (IDuplexTypedMessageReceiver<ResponseMessage^, RequestMessage^>^)sender;
    aReceiver->SendResponseMessage(e->ResponseReceiverId, aResponseMessage);

}

Above code is the server code for the RPC using Protocol Buffer but every but client is not able get any response from server same code is working in C# but it is not working with C++...I think that we can write a code equiant to C# code in C++/CLI.But this code is not working i client is working fine with the C# code. I noticed that there OnMessageRecived() function is not getting called.

Here is the example for the native C++ and C# server code but i want to do it with pure managed C++ http://eneter.blogspot.in/2013/12/native-c-how-to-communicate-with-net.html

Any body can tell me whats going on...How can i resolve the problem.


Solution

  • I am not 100% sure but I think the problem in your case could be missing attribute [ProtoContract] on declarations of RequestMessage and ResponseMessage.

    E.g. on service side:

        [ProtoContract]
        public ref class RequestMessage
        {
        public:
            [ProtoMember(1)]
            Int32 Number1;
            [ProtoMember(2)]
            Int32 Number2;
        };
    

    and then on the client side:

        [ProtoContract]
        public class RequestMessage
        {
            [ProtoMember(1)]
            public int Number1 { get; set; }
            [ProtoMember(2)]
            public int Number2 { get; set; }
        }