Hello,
I tried to implement a two way communication via remoting. I would like to do it via an observer pattern.
So, I have on Shared dll, which contains an Observer interface and a MessageServiceInterface.
The server implements the Observer interface and the clien too. The MessageService is implemented by
my server and it shared the object via remoting.
So far erverything works. I can add the server Observer and everything works fine, but I can't add the Client obser via
Marshal object.
I get the following runtime exception: Analysis failed. The XML-Key 'a1:http://schemas.microsoft.com/clr/nsassem/Client/Client%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull MessageHandler' isn't assigned to any assembly. (Something similar I have to translate it ;-))
Client:
using System;
using System.Runtime.Remoting;
using Shared;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using Server;
namespace Client
{
[Serializable]
class Client : Observer
{
[NonSerialized]
private IMessageService service;
public Client()
{
HttpChannel channel = new HttpChannel();
ChannelServices.RegisterChannel(channel, false);
service = (IMessageService)Activator.GetObject(typeof(IMessageService), "http://localhost:1234/MessageService.soap");
}
static void Main(string[] args)
{
Client client = new Client();
client.registerObserver();
client.read();
}
public void read()
{
for (; ; )
{
String msg = Console.ReadLine();
service.SendMessageToAll(msg);
}
}
public void update(String msg)
{
Console.WriteLine(msg);
}
public void registerObserver()
{
service.addObserver(this);
}
}
}
Server:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization.Formatters;
using System.Collections.Generic;
using Shared;
namespace Server
{
class ServerStartup : Observer
{
private IMessageService service;
public ServerStartup()
{
service = new MessageService();
service.addObserver(this);
HttpChannel channel = new HttpChannel(1234);
ChannelServices.RegisterChannel(channel, false);
/*RemotingConfiguration.RegisterWellKnownServiceType(
typeof(CustomerManager),
"CustomerManager.soap",
WellKnownObjectMode.Singleton); */
RemotingServices.Marshal(service, "MessageService.soap");
}
static void Main(string[] args)
{
ServerStartup startup = new ServerStartup();
for (; ; )
{
String msg = Console.ReadLine();
startup.sendMessage(msg);
}
}
public void update(String msg)
{
Console.WriteLine(msg);
}
public void sendMessage(String msg)
{
service.SendMessageToAll(msg);
}
}
}
MessageServer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shared;
namespace Server
{
[Serializable]
public class MessageService : IMessageService
{
private List<Observer> observers;
public MessageService()
{
observers = new List<Observer>();
}
public override void SendMessageToAll(string message)
{
foreach(Observer observer in observers)
{
observer.update(message);
}
}
public override void addObserver(Observer observer)
{
observers.Add(observer);
}
public override void removeObserver(Observer observer)
{
observers.Remove(observer);
}
}
}