There may be a simpler way to do this, and I donât have a remote machine to run this against (Iâm using IIS locally), but what occurred to me was creating a server object running on the remote machine, and using a client proxy object to invoke System.IO.File.Exists on the remote object over an HTTP Channel.
To do that I built a solution with three projects â a console app (Client), a class library (ServerSideDLL) containting just a RemoteFileChecker class exposed via remoting, and a Web project (RemotingHost) running on IIS that gives ServerSideDLL a remote process to run in.
1. Create a blank solution, name it whatever you like.
2. Add a class library project named ServerSideDLL.
3. Add a class, and name it RemoteFileChecker. Hereâs the code:
Code:
using System;
namespace ServerSideDLL
{
public class RemoteFileChecker : MarshalByRefObject
{
public string GetServerName() {
return System.Environment.MachineName;
}
public int GetProcessID(){
return System.Diagnostics.Process.GetCurrentProcess().Id;
}
public bool FileExists(string fileName){
return System.IO.File.Exists(fileName);
}
}
}
4. Add an Empty Web project to the solution and name it RemotingHost.
5. Add a reference to the ServerSideDLL library project.
6. Add a Web.config file, and configure remoting via a <system.runtime.remoting> element, like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="SingleCall"
objectUri="ServerSideDLL.Rem"
type="ServerSideDLL.RemoteFileChecker, ServerSideDLL"/>
</service>
</application>
</system.runtime.remoting>
<system.web>
â¦
7. Build the solution. The remoting server should be ready to use. Test it by typing the following in a browser to view RemotingHost's WSDL specification.
http://localhost/remotinghost/serversidedll.rem?wsdl
8. Add a Console Application to the solution and name it Client.
9. Add a reference to the ServerSideDLL library project.
10. Here is the client code:
Code:
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
namespace Client
{
class Program
{
static void Main()
{
Hashtable properties = new Hashtable();
properties.Add("name", "HttpBinary");
BinaryClientFormatterSinkProvider formatter =
new BinaryClientFormatterSinkProvider();
HttpChannel channel =
new HttpChannel(properties, formatter, null);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(ServerSideDLL.RemoteFileChecker),
"http://localhost/remotinghost/serversidedll.rem");
System.Text.StringBuilder output = new System.Text.StringBuilder();
ServerSideDLL.RemoteFileChecker remoteFileChecker = new ServerSideDLL.RemoteFileChecker();
// Identify remote process the ServerSideDLL assembly is running in.
output.AppendFormat("ServerSideDLL values\n");
output.AppendFormat("Machine: {0}\n", remoteFileChecker.GetServerName());
output.AppendFormat("Process: {0}\n", remoteFileChecker.GetProcessID());
// Identify the local process the Client assembly is running in.
output.AppendFormat("\nClient values\n");
output.AppendFormat("Machine: {0}\n", System.Environment.MachineName);
output.AppendFormat("Process: {0}\n",
System.Diagnostics.Process.GetCurrentProcess().Id);
// Use remote object to verify files existence.
output.AppendFormat("\nFile located by remote object: {0}",
remoteFileChecker.FileExists(@"\Inetpub\wwwroot\RemotingHost\bin\TheFile.txt"));
Console.WriteLine(output.ToString(), "Client");
Console.Read();
}
}
}
I just stuck a text file named âTheFileâ in RemotingHostâs bin directory.
11. Set the Client project as the Solutions Startup Project.
12. Hereâs my output:
Code:
ServerSideDLL values
Machine: ELIOT
Process: 2772
Client values
Machine: ELIOT
Process: 3744
File located by remote object: True
As I mentioned, Iâm not actually accessing a remote physical machine, just communicating across process boundaries. But since thatâs basically what âaccessing a remote physical machineâ entails, I suspect this would work. If this is completely wrong-headed, someome feel free to scold me.
And a bow to Rockford Lhotka for his wonderful explanations of the remoting subsystem.
HTH,
Bob