Dynamically invoke a method over a socket - c#

I have 2 pc's with some software, now I need one pc to execute a method on another pc, now I've searched high and low but could not find anything on how to do this. I could do this with writing my own little interface that serializes the arguments, method name and return objects and sending this over a socket then deserialize this execute the method using reflection, and return an result object over the socket. But I would like someone else's opinion before I start writing something that is much easier another way.
Send multiple arguments (they will all be received and send as an object)
Return an object
serialize back an exception object if any has occurred
I have not done anything in serializing objects and sending them over a socket, but are all standard objects serializable? Like a List<> array[] float dateTime?
I hope to have explained this ok, if not I'm sorry and ask what is not clear.

Create service WCF and config WCF to work over TCP.
This will give you most things 'out of the box' (serialize /deserialize, open/close socket )
There are good examples here, here and good reading here

I have searched the internet for examples and pasted together some code, i'm posting it here if somone needs it too. This is a dirty code but it works, the InvokeMethod is on the client side, and the startIBC is what needs to be started on each server:
[ServiceContract]
public interface IBlissRequest
{
[OperationContract]
object SystemRequest(string InstanceName, string MethodName, params object[] Parameters);
}
public class BlissRequest : IBlissRequest
{
public object SystemRequest(string InstanceName, string MethodName, params object[] Parameters)
{
return System21.BlissProcessingUnit.BPU.RequestFromIBC(InstanceName, MethodName, Parameters);
}
}
public static object InvokeMethod(string targetIpAddress, string InstanceName, string MethodName, params object[] Parameters)
{
try
{
var ep = "net.tcp://" + targetIpAddress + ":9985/IBC";
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
ChannelFactory<IBlissRequest> pipeFactory = new ChannelFactory<IBlissRequest>(binding, new EndpointAddress(ep));
IBlissRequest pipeProxy = pipeFactory.CreateChannel();
return pipeProxy.SystemRequest(InstanceName, MethodName, Parameters);
}
catch
{
BPUConsole.WriteLine(BPUConsole.WriteSource.IBC, "Unable to execute method: '" + MethodName +"' on Instance: '"+InstanceName+"' becouse IBC is unable to connect to: "+ targetIpAddress);
throw new Exception("Unable to connect to: " + targetIpAddress);
}
}
public static void StartIBC()
{
var uri = "net.tcp://" + BlissProcessingUnit.BPUInformation.LocalIpAddresses[0] + ":9985";
Console.WriteLine("Opening connection on: " + uri);
ServiceHost host = new ServiceHost(typeof(BlissRequest), new Uri[] { new Uri(uri) });
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
host.AddServiceEndpoint(typeof(IBlissRequest), binding, "IBC");
host.Open();
Console.WriteLine("Service is available. " + "Press <ENTER> to exit.");
}

What you are describing sounds like remote procedure calling (RPC). RPC allows you to create a single object on a server which clients can interact with as if it were a local object (so completely avoid the need to deal with sockets). Alternatively each client can also create its own unique server object to interact with.
A full implementation of RPC can be found in the network library networkcomms.net. The following code snippet is taken from the available RPC example and uses an object of type MathClass that can perform simple maths calculations.
The object exists server side:
//Register a single object server side called "Calculator"
RemoteProcedureCalls.Server.RegisterInstanceForPublicRemoteCall<MathClass, IMath>(new MathClass(), "Calculator");
On the client side:
//Get a reference to the remote object named "Calculator"
IMath calc = RemoteProcedureCalls.Client.CreateProxyToPublicNamedInstance<IMath>(connection, "Calculator", out instanceId);
//We can now use the calculator object as if it were local
//The following WriteLine outputs '12' where the calculation was performed on the server
Console.WriteLine(calc.Multiply(4, 3));
Disclaimer: I have to add that I am a developer for this library.

Related

Passing a stream to a method via .NET remoting

Let me briefly describe the situation.
First, I have a WCF RestFul webservice with this method :
[WebInvoke(UriTemplate = "/Dynamic/{*sParameter}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public string ExecuteWebAPIRequest(string sParameter, Stream streamPost)
{
...
var oClientFormatSinkProvider = new BinaryClientFormatterSinkProvider();
IDictionary aoProps = new Hashtable();
aoProps["port"] = 4626;
aoProps["timeout"] = "-1";
aoProps["name"] = "clientChan";
TcpClientChannel channel = new TcpClientChannel(aoProps, oClientFormatSinkProvider);
ChannelServices.RegisterChannel(channel, false);
//-- Call Bridge
string result = GetBridgeObject().ExecuteWebAPIRequest(sIpAddress, streamPost, sParameter);
//-- Return result
return result ?? "";
}
here is the content of the GetBridgeObject() method (that would be the remoting client):
private IBridge GetBridgeObject()
{
return (IBridge)Activator.GetObject(typeof(IBridge), "tcp://localhost:4626/RemoteBridge");
}
Next, there is the bridge process containing this method (that would be the remoting server):
public void StartService()
{
//-- Initialize .NET remoting
var oServerFormatSinkProvider = new BinaryServerFormatterSinkProvider();
oServerFormatSinkProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary aoProps = new Hashtable();
aoProps["port"] = 4626;
aoProps["timeout"] = "-1";
aoProps["name"] = "serverChan";
TcpServerChannel channel = new TcpServerChannel(aoProps, oServerFormatSinkProvider);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof (Bridge), "RemoteBridge", WellKnownObjectMode.Singleton);
}
And finaly, in the remote bridge object, this method :
public string WUPP_ExecuteWebAPIRequestI(string sPPInstanceName, Stream oInputStream, string sParameter)
{
...
int read = oInputStream.Read(buffer, 0, buffer.Length); //That's where the problem is
...
}
As stated in the code snippet, the problem occurs when I try to read the stream, i get this error :
Exception: System.Runtime.Remoting.RemotingException: This remoting proxy has no channel sink which means either the server has no registered server channels that are listening, or this application has no suitable client channel to talk to the server.
at System.Runtime.Remoting.Proxies.RemotingProxy.InternalInvoke(IMethodCallMessage reqMcmMsg, Boolean useDispatchMessage, Int32 callType)
at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(IMessage reqMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at System.IO.Stream.Read(Byte[] buffer, Int32 offset, Int32 count)
I know for sure I can pass streams via .NET remoting because in the bridge, there are other methods that return streams and that work well.
I guess the problem is somewhere in the remoting server or client when I register the channels but after two days of research and tests, i still haven't found an answer.
Thanks in advance for your help.
Since I had a similar problem and needed a solution as well, I finally found a solution: the client channel needs to be registered differently to make it work.
// Creating a custom formatter for a TcpChannel sink chain.
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("typeFilterLevel", "Full");
dict.Add("port", "0");
ChannelServices.RegisterChannel(new TcpChannel(dict, clientProvider, provider), false);
The interessting things here are
the channel is bidirectional
port 0 tells remoting framework to decide which port it should use.
for up- and download of streams the typeFilterLevel Full needs to be
set.
Would be interested to know why my answer got a downvote as the solution works in production code.

.NET Remoting and Server Activated Objects

what's the problem with the following code...
I have this Complex class:
public class Complex : MarshalByRefObject
{
public double imaginary{get;set;}
public double real{get;set;}
public void setReal(double re)
{
real = re;
}
public void setImaginary(double im)
{
imaginary = im;
}
public Complex(double im, double re)
{
imaginary = im;
real = re;
}
public void writeMembers()
{
Console.WriteLine(real.ToString() + imaginary.ToString());
}
}
Actually, there's a little more to it, but the code it's too big, and we don't use the rest of it in the context of this.
Then, I implemented a server which listens for connections:
HttpChannel channel = new HttpChannel(12345);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(SharedLib.Complex), "ComplexURI", WellKnownObjectMode.SingleCall);
Console.WriteLine("Server started. Press any key to close...");
Console.ReadKey();
foreach (IChannel ichannel in ChannelServices.RegisteredChannels)
{
(ichannel as HttpChannel).StopListening(null);
ChannelServices.UnregisterChannel(ichannel);
}
Then, we have the client:
try
{
HttpChannel channel = new HttpChannel();
RemotingConfiguration.Configure("Client.exe.config", false);
Complex c1 = (Complex)Activator.GetObject(typeof(Complex), "http://localhost:12345/ComplexURI");
if (RemotingServices.IsTransparentProxy(c1))
{
c1.real = 4;
c1.imaginary = 5;
c1.writeMembers();
Console.ReadLine();
}
else
{
Console.WriteLine("The proxy is not transparent");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
Then, I run the server, which opens a console window, and I run the client.
Instead of displaying 4 and 5 on the server window, I merely get 00, a sign that the members weren't changed.
How do I do, so the members change?
Thanks.
The problem is that you're using WellKnownObjectMode.SingleCall. As the documentation says:
SingleCall Every incoming message is serviced by a new object instance.
Singleton Every incoming message is serviced by the same object instance.
See also the documentation for RegisterWellKnownServiceType:
When the call arrives at the server, the .NET Framework extracts the URI from the message, examines the remoting tables to locate the reference for the object that matches the URI, and then instantiates the object if necessary, forwarding the method call to the object. If the object is registered as SingleCall, it is destroyed after the method call is completed. A new instance of the object is created for each method called.
In your case, the statement c.Real = 4 is a call to the Real property setter. It makes a call to the remote object, which creates a new object, sets the Real property to 4, and returns. Then when you set the imaginary property, it creates a new object, etc.
If you want this to work, you'll have to use WellKnownObjectMode.Singleton. But you might want to ask yourself if you really want such a "chatty" interface. Every time you set a property, it requires a call through the proxy to the server.
And, finally, you might consider abandoning Remoting altogether. It's old technology, and has a number of shortcomings. If this is new development, you should be using Windows Communications Foundation (WCF). The Remoting documentation says:
This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF).

Programming a distributed application written in C#, Ruby and Java using XML-RPC

I am tasked with writing a distributed event managing tool where each client, either a Ruby, C# or Java Client, synchronises all changes with a list of registered clients. I have to use XML-RPC to achieve the goal. My team and I have written up an XML-RPC client and server in each language and will provide the relevant source code below. If you require more code, please let me know.
The problem is that I can get Java and C# to communicate with each other. Ruby can communicate with the others but C# (and maybe Java, haven't tested yet) have problems addressing the Ruby server. I guess the problem is with the Endpoint. First let me give you some code. When reading please be aware that the code is actually written by a team and naming conventions differ a bit:
C# client
Uri _address = new Uri("http://" + _s + ":8000/xmlrpc/EventManagerService");
ChannelFactory<IEventManagerWCF_XML_RPC> _factory = new ChannelFactory<IEventManagerWCF_XML_RPC>(new WebHttpBinding(WebHttpSecurityMode.None), new EndpointAddress(_address));
_factory.Endpoint.Behaviors.Add(new XmlRpcEndpointBehavior());
IEventManagerWCF_XML_RPC _proxy = _factory.CreateChannel();
_proxy will not hold the client for a given URI. Those are stored in a dictionary and used when the need arises to synchronise events. One such synchronisation would happen in the case of a modification;
foreach(IEventManagerWCF_XML_RPC proxy in this.peers.Values)
proxy.modify(_id, _field, _newValue);
Here is an extract from the IEventManagerWCF_XML_RPC interface;
[OperationContract(Action = "EventManagerService.modify")]
bool modify(int id, string field, string newValue);
C# XML RPC service
Uri _baseAddress = new Uri("http://localhost:8000/xmlrpc");
_eventManagerHost = new ServiceHost(typeof(EventManagerService), _baseAddress);
try
{
ServiceEndpoint epXmlRpc = _eventManagerHost.AddServiceEndpoint(typeof(IEventManagerWCF_XML_RPC), new WebHttpBinding(WebHttpSecurityMode.None), "EventManagerService");
epXmlRpc.Behaviors.Add(new XmlRpcEndpointBehavior());
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
_eventManagerHost.Description.Behaviors.Add(smb);
_eventManagerHost.Open();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
_eventManagerHost.Abort();
}
Nothing special here I guess. Lets move on to the Java code!
Java Client
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
try {
config.setServerURL(new URL("http://"+ip+":8000/xmlrpc/EventManagerService"));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
config.setEnabledForExtensions(true);
config.setConnectionTimeout(60 * 1000);
config.setReplyTimeout(60 * 1000);
XmlRpcClient client = new XmlRpcClient();
client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
client.setConfig(config);
xmlRpcPeers.put(ip, client);
xmlRpcPeers now holds the different clients. They are called as follows;
for(XmlRpcClient peer : this.xmlRpcPeers.values())
{
try {
peer.execute("EventManagerService.modify", params);
} catch (Exception e) {
e.printStackTrace();
}
}
The Java Server has it's own class and is instantiated with a simple new call;
public class Server extends Thread{
/**
* Server port
*/
private static final int port = 8000;
/**
* Starts the XML-RPC server
*/
public void run(){
WebServer webServer = new WebServer(port);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
try
{
phm.addHandler("EventManagerService", lu.uni.binfo.ds.EventManager_Java.EventManagerService.class);
}
catch (XmlRpcException e1)
{
e1.printStackTrace();
}
xmlRpcServer.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
try
{
webServer.start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Up till now everything seemed to work fine. Adding Ruby to the mix is what gives the most trouble. Here is the relevant code;
Ruby Client
Ruby clients are also stored in a dictionary. It is populated as follows;
#devices_XMLRPC[key] = EventManagerClient_XMLRPC.new(tokens[0]).device
The code for the class is:
class EventManagerClient_XMLRPC
#uri
#device
attr_accessor :uri, :device
def initialize(uri)
#uri = uri
#device = XMLRPC::Client.new2(
uri="http://" << #uri.to_s << ":8000/xmlrpc/EventManagerService", proxy=nil, timeout=30)
end
end
A call to synchronise on modifications looks like this:
#devices_XMLRPC.each_value { |client| client.call("EventManagerService.modify", tokens[0], field, value) }
Ruby Server
server = XMLRPC::Server.new(8000, "127.0.0.1")
server.add_handler("xmlrpc/EventManagerService", EventManagerService.instance)
puts "Server ready!"
server.serve
The EventManagerService class:
class EventManagerService
include Singleton
#manager
def initialize()
#manager = EventManager.instance
end
def modify(id, field, newValue)
puts "modify called"
#manager.modify([id, field, newValue], 1)
end
end
EventManager being the class where all the logic resides.
The error when trying to communicate from C# to Ruby is an EndPointNotFoundException that reads:
There was no endpoint listening at http://ip:8000/xmlrpc/EventManagerService that could accept the message.[...]
I tried fiddling around with the endpoint declaration but cannot seem to get it to work. The Ruby documentation does not help either. I am in need of help!
You could try inspecting the traffic that goes over the line with WireShark. With this tool, you would be able to see the actual HTTP data that is transferred between the different applications. Maybe this provides a hint as to why you have problems communicating between Ruby and C#.

Unable to read data from the transport connection: the connection was closed

The exception is Remoting Exception - Authentication Failure. The detailed message says "Unable to read data from the transport connection: the connection was closed."
I'm having trouble with creating two simple servers that can comunicate as remote objects in C#. ServerInfo is just a class I created that holds the IP and Port and can give back the address. It works fine, as I used it before, and I've debugged it. Also the server is starting just fine, no exception is thrown, and the channel is registered without problems. I'm using Forms to do the interfaces, and call some of the methods on the server, but didn't find any problems in passing the parameters from the FormsApplication to the server when debugging. All seems fine in that chapter.
public ChordServerProgram()
{
RemotingServices.Marshal(this, "PADIBook");
nodeInt = 0;
}
public void startServer()
{
try
{
serverChannel = new TcpChannel(serverInfo.Port);
ChannelServices.RegisterChannel(serverChannel, true);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
I run two instances of this program. Then startNode is called on one of the instances of the application. The port is fine, the address generated is fine as well. As you can see, I'm using the IP for localhost, since this server is just for testing purposes.
public void startNode(String portStr)
{
IPAddress address = IPAddress.Parse("127.0.0.1");
Int32 port = Int32.Parse(portStr);
serverInfo = new ServerInfo(address, port);
startServer();
//node = new ChordNode(serverInfo,this);
}
Then, in the other istance, through the interface again, I call another startNode method, giving it a seed server to get information from. This is where it goes wrong. When it calls the method on the seedServer proxy it just got, a RemotingException is thrown, due to an authentication failure. (The parameter I'll want to get is the node, I'm just using the int to make sure the ChordNode class has nothing to do with this error.)
public void startNode(String portStr, String seedStr)
{
IPAddress address = IPAddress.Parse("127.0.0.1");
Int32 port = Int32.Parse(portStr);
serverInfo = new ServerInfo(address, port);
IPAddress addressSeed = IPAddress.Parse("127.0.0.1");
Int32 portSeed = Int32.Parse(seedStr);
ServerInfo seedInfo = new ServerInfo(addressSeed, portSeed);
startServer();
ChordServerProgram seedServer = (ChordServerProgram)Activator.GetObject(typeof(ChordServerProgram), seedInfo.GetFullAddress());
// node = new ChordNode(serverInfo,this);
int seedNode = seedServer.nodeInt;
// node.chordJoin(seedNode.self);
}
Try setting the ensureSecurity to false, and it should start working.
ChannelServices.RegisterChannel(serverChannel, false);
You've specified that security is a must on your Remoting server in startServer() with:
ChannelServices.RegisterChannel(serverChannel, true);
Yet the 'client' end does not specify security, hence the authorisation error. You need to specify tcp channel security on both ends unless the server security setting is set to 'false'. In your second startNode method you need to do the following before using Activator.GetObject, note no port specified on the TcpChannel unlike the server end:
TcpChannel ClientChan = new TcpChannel();
ChannelServices.RegisterChannel(ClientChan, true);
Furthermore, unless you're doing it in some code you haven't given us, you also do not seem to have registered a well known service type server side, although you say it's been working in the debugger so maybe that's not necessary in the case. See MSDN on RegisterWellKnownServiceType.

Pass a remote object to a remote method in C#

I'm attempting to pass a remote object as a parameter to a remote method, but I get a security exception when the remote object attempts to run a method on the received remote object.
This is a sample remote object:
public class SharpRemotingDLL : MarshalByRefObject
{
public String getRemoteMessage(SharpRemotingDLL server)
{
// the exception is raised here
return server.getMessage();
}
public String getMessage()
{
return "Hello World!";
}
}
This is the server starter (two instances of this are running, one on 127.0.0.10025, the other on 127.0.0.10026):
public static int Main(string[] args)
{
TcpServerChannel channel;
channel = new TcpServerChannel(10025);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SharpRemotingDLL),
"SharpRemotingDLL",
WellKnownObjectMode.Singleton);
Console.ReadLine();
return 0;
}
And this is the client:
static void Main(string[] args)
{
SharpRemotingDLL server0 = (SharpRemotingDLL)
Activator.GetObject(typeof(SharpRemotingDLL),
"tcp://localhost:10025/SharpRemotingDLL");
SharpRemotingDLL servers[1] = (SharpRemotingDLL)
Activator.GetObject(typeof(SharpRemotingDLL),
"tcp://localhost:10026/SharpRemotingDLL");
Console.WriteLine(server0.getRemoteMessage(server1));
}
How do I correctly pass server1 as a parameter to the getRemoteMessage method?
vbigiani,
This article should shed some light on your problem:
http://weblogs.asp.net/jan/archive/2003/06/01/8106.aspx
If that doesn't work, you can get several relevant Google hits here:
Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed
I have written this kind of code before, but I have never tried to pass a server object to another server object in this way. I'm not a security expert, but I can sense why this might not be allowed.
You should obtain the message directly from Server1, rather than asking another remote server to return it. In other words, your message retrieval logic needs to be in the client.

Categories