I am trying to port my remote access code in .net to wcf, but it is difficult for me. Can someone help me migrate this simple Remoting-based program below to use WCF? The program implements a simple publisher / subscriber template in which we have one TemperatureProviderProgram program, which is the publisher of many TemperatureSubcriberPrograms programs that subscribe to ThermProvider.
To run programs:
- Copy the TemperatureProviderProgram and TemperatureSubcriberProgram to separate console application projects.
- Copy the remaining classes and interfaces into a common class library project, then add a link to the System.Runtime.Remoting library
- Add a link to the class library project from console application projects.
- Observe and run 1 TemperatureProviderProgram and several TemperatureSubcriberProgram.
Please note that IIS or xml should not be used. Thanks in advance.
public interface ITemperatureProvider { void Subcribe(ObjRef temperatureSubcriber); } [Serializable] public sealed class TemperatureProvider : MarshalByRefObject, ITemperatureProvider { private readonly List<ITemperatureSubcriber> _temperatureSubcribers = new List<ITemperatureSubcriber>(); private readonly Random randomTemperature = new Random(); public void Subcribe(ObjRef temperatureSubcriber) { ITemperatureSubcriber tempSubcriber = (ITemperatureSubcriber)RemotingServices.Unmarshal(temperatureSubcriber); lock (_temperatureSubcribers) { _temperatureSubcribers.Add(tempSubcriber); } } public void Start() { Console.WriteLine("TemperatureProvider started..."); BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider(); provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; TcpServerChannel tcpChannel = new TcpServerChannel("TemperatureProviderChannel", 5001, provider); ChannelServices.RegisterChannel(tcpChannel, false); RemotingServices.Marshal(this, "TemperatureProvider", typeof(ITemperatureProvider)); while (true) { double nextTemp = randomTemperature.NextDouble(); lock (_temperatureSubcribers) { foreach (var item in _temperatureSubcribers) { try { item.OnTemperature(nextTemp); } catch (SocketException) {} catch(RemotingException) {} } } Thread.Sleep(200); } } } public interface ITemperatureSubcriber { void OnTemperature(double temperature); } [Serializable] public sealed class TemperatureSubcriber : MarshalByRefObject, ITemperatureSubcriber { private ObjRef _clientRef; private readonly Random portGen = new Random(); public void OnTemperature(double temperature) { Console.WriteLine(temperature); } public override object InitializeLifetimeService() { return null; } public void Start() { BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider(); provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; int port = portGen.Next(1, 65535); TcpServerChannel tcpChannel = new TcpServerChannel(string.Format("TemperatureSubcriber_{0}", Guid.NewGuid()), port, provider); ChannelServices.RegisterChannel(tcpChannel, false); ITemperatureProvider p1 = (ITemperatureProvider)RemotingServices.Connect(typeof(ITemperatureProvider), "tcp://localhost:5001/TemperatureProvider"); _clientRef = RemotingServices.Marshal(this, string.Format("TemperatureSubcriber_{0}_{1}.rem", Environment.MachineName, Guid.NewGuid())); p1.Subcribe(_clientRef); } } public class TemperatureProviderProgram { static void Main(string[] args) { TemperatureProvider tp = new TemperatureProvider(); tp.Start(); } } public class TemperatureSubcriberProgram { static void Main(string[] args) { Console.WriteLine("Press any key to start TemperatureSubcriber."); Console.ReadLine(); TemperatureSubcriber ts = new TemperatureSubcriber(); ts.Start(); Console.ReadLine(); } }
remotingDev
source share