public void WriteRequestAndReadResponse() { //var properties = new Hashtable() { { "port", 8080 } }; //var channel = new TcpChannel(properties, null, new SoapServerFormatterSinkProvider()); //if using SOAP via TCP, messageRequestStream must be SOAP format var channel = new TcpChannel(8080); ChannelServices.RegisterChannel(channel, false); var service = new ServiceClass(); ObjRef obj = RemotingServices.Marshal(service, "Remote"); var uri = "tcp://localhost:8080/Remote"; using (var client = new TcpClient()) { client.Connect("localhost", 8080); using (var stream = client.GetStream()) { var messageRequest = new MethodCall(new Header[] { new Header("__Uri", uri), new Header("__MethodName", "Do"), new Header("__MethodSignature", new Type[] { typeof(string) }), new Header("__TypeName", typeof(ServiceClass).AssemblyQualifiedName), new Header("__Args", new object[] { "Hi" }) }); var messageRequestStream = BinaryFormatterHelper.SerializeObject(messageRequest); var writer = new ProtocolWriter(stream); writer.WritePreamble(); writer.WriteMajorVersion(); writer.WriteMinorVersion(); writer.WriteOperation(TcpOperations.Request); writer.WriteContentDelimiter(TcpContentDelimiter.ContentLength); writer.WriteContentLength(messageRequestStream.Length); writer.WriteTransportHeaders(uri); writer.WriteBytes(messageRequestStream); var reader = new ProtocolReader(stream); Console.WriteLine("Preamble: {0}", reader.ReadPreamble()); Console.WriteLine("MajorVersion: {0}", reader.ReadMajorVersion()); Console.WriteLine("MinorVersion: {0}", reader.ReadMinorVersion()); var op = reader.ReadOperation(); Assert.AreEqual(TcpOperations.Reply, op); Console.WriteLine("Operation: {0}", op); Console.WriteLine("ContentDelimiter: {0}", reader.ReadContentDelimiter()); var length = reader.ReadContentLength(); Console.WriteLine("ContentLength: {0}", length); reader.ReadTransportHeaders(); var messageResponse = BinaryFormatterHelper.DeserializeObject(reader.ReadBytes(length)) as MethodResponse; Assert.IsNotNull(messageResponse); DumpHelper.DumpMessage(messageResponse); if (messageResponse.Exception != null) { throw messageResponse.Exception; } Assert.AreEqual("Hi", messageResponse.ReturnValue); } } }
public void WriteRequestAndReadResponse() { //var properties = new Hashtable() { { "port", 8080 } }; //var channel = new TcpChannel(properties, null, new SoapServerFormatterSinkProvider()); //if using SOAP via TCP, messageRequestStream must be SOAP format var channel = new TcpChannel(8080); ChannelServices.RegisterChannel(channel, false); var service = new ServiceClass(); ObjRef obj = RemotingServices.Marshal(service, "Remote"); var uri = "tcp://localhost:8080/Remote"; using (var client = new TcpClient()) { client.Connect("localhost", 8080); using (var stream = client.GetStream()) { var messageRequest = new MethodCall(new Header[] { new Header("__Uri", uri), new Header("__MethodName", "Do"), new Header("__MethodSignature", new Type[] { typeof(string) }), new Header("__TypeName", typeof(ServiceClass).AssemblyQualifiedName), new Header("__Args", new object[] { "Hi" }) }); var messageRequestStream = BinaryFormatterHelper.SerializeObject(messageRequest); var writer = new ProtocolWriter(stream); writer.WritePreamble(); writer.WriteMajorVersion(); writer.WriteMinorVersion(); writer.WriteOperation(TcpOperations.Request); writer.WriteContentDelimiter(TcpContentDelimiter.ContentLength); writer.WriteContentLength(messageRequestStream.Length); writer.WriteTransportHeaders(uri); writer.WriteBytes(messageRequestStream); var reader = new ProtocolReader(stream); Console.WriteLine("Preamble: {0}", reader.ReadPreamble()); Console.WriteLine("MajorVersion: {0}", reader.ReadMajorVersion()); Console.WriteLine("MinorVersion: {0}", reader.ReadMinorVersion()); var op = reader.ReadOperation(); Assert.AreEqual(TcpOperations.Reply, op); Console.WriteLine("Operation: {0}", op); Console.WriteLine("ContentDelimiter: {0}", reader.ReadContentDelimiter()); var length = reader.ReadContentLength(); Console.WriteLine("ContentLength: {0}", length); reader.ReadTransportHeaders(); var messageResponse = BinaryFormatterHelper.DeserializeObject(reader.ReadBytes(length)) as MethodResponse; Assert.IsNotNull(messageResponse); DumpHelper.DumpMessage(messageResponse); if (messageResponse.Exception != null) throw messageResponse.Exception; Assert.AreEqual("Hi", messageResponse.ReturnValue); } } }