Et eksempel på remoting
Jeg er ved at implementere et eksempel i remoting, som jeg har fundet på netet. Men jeg kan ikke få det til at virke. Jeg benytter ok ikke den rigtige fremgangsmåde. Dette er opskriften, som jeg har fulgt:A Complete Remoting Example
Let's look at an example of a remote component that provides the time on the server. Such a component could be used to synchronize clients with a central server.
Server Class
We'll need to first write the server class, which provides the implementation of methods to be called remotely. As mentioned previously, a component that is intended to be called remotely must derive from MarshalByRefObject. A single method, GetServerTime(), will return the server time, formatted as a string so that we can display it on the client. Here is the complete sourcecode for our server component:
using System;
namespace RemotingExample
{
//Remote Server Class
public class TimeServer : MarshalByRefObject
{
//Method to Be Called Remotely By Client
public string GetServerTime()
{
return DateTime.Now.ToString();
}
}
}
Server Wrapper
Once we have the Server class implemented, we need to provide a wrapper class that registers a new channel for requests, and indicates the server class containing the remote method implementations. [Note: If you are using Visual Studio.NET, you will need to add a reference to the System.Runtime.Remoting assembly.]
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
namespace RemotingExample
{
public class TimeServerWrapper
{
public static void Main()
{
//Register New Channel
TcpChannel objChannel = new TcpChannel(8765);
ChannelServices.RegisterChannel(objChannel);
//Register Server
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("RemotingExample.TimeServer,timeserver"), "TimeServerURI", WellKnownObjectMode.SingleCall);
//Wait for Connections
Console.WriteLine("Waiting for connections. Press any key to exit.");
Console.ReadLine();
}
}
}
Client
A client calling a remote component needs to register a channel on it's end for the communication, and then use the Activator object to get an instance of the component.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
namespace RemotingExample
{
public class TimeServerWrapper
{
public static void Main()
{
//Register New Channel
TcpChannel objChannel = new TcpChannel();
ChannelServices.RegisterChannel(objChannel);
//Get Instance of Server Object
TimeServer objOurTimeServer = (TimeServer)Activator.GetObject(typeof(RemotingExample.TimeServer), "tcp://localhost:8765/TimeServerURI");
//Call Remote Method
Console.WriteLine(objOurTimeServer.GetServerTime());
}
}
}
The following batch file is used to compile all three applications:
csc /t:library /debug /r:System.Runtime.Remoting.dll TimeServer.cs
csc /t:exe /r:TimeServer.dll /debug /r:System.Runtime.Remoting.dll TimeServerWrapper.cs
csc /t:exe /r:TimeServer.dll /debug /r:System.Runtime.Remoting.dll TimeClient.cs
In order to run the example, we first must run the TimeServerWrapper server executable (TimeServerWrapper.exe) to start listening for connections.
Next, in another command window, we run TimeClient.exe. Assuming the correct port numbers and object URIs are configured, the client will display the time on the server.
Once you have a server wrapper that uses the HttpChannel, try this exercise. Start up the server wrapper so that it is listening for connections. Now, open a browser, and enter in the location of the service, as set by your port number and object URI, followed by ?WSDL. In a modified version of our TcpChannel example, this would be "http://localhost:8765/TimeServerURI?WSDL". The screenshot below shows the WSDL for our server component. If you think about it, a remotely activated component over HTTP is the definition of a web service.
Hvad gør jeg forkert? Kendes en let og ligetil fremgangsmåde på dansk?