c# remote events

北城余情 提交于 2020-01-03 01:59:05

问题


I'm trying to do a server client application to learn more about remote programs using c#.

The structure is quite simple.

I have a server which triggers an event when the function updateInventory is called and each client registers a function to the event (using a delegate).

So when any of the client updates the inventory (by calling the updateInventory function) all the clients are warned about it by ways of the event.

Now the problem is that it simply isn't working, when a client calls the updateInventory function the one in the server isn't evoked.

The program has the following 4 componenents:

The inventoryChangedArgs

Which is Serializable and serves as argument to the function triggered by the event. I compiled it to .dll and used on the server and the client by "add reference...":

using System;

[Serializable]
public class InventoryChangeArgs:EventArgs
{
    private string pno;
    public string Pno
    {
        get
        {
            return pno;
        }
        set
        {
            pno = value;
        }
    }
    private int change;
    public int Change
    {
        get
        {
            return change;
        }
        set
        {
            change = value;
        }
    }
    public InventoryChangeArgs(string pno, int change)
    {
        Console.WriteLine("InventoryChangeArgs constructed");
        this.pno = pno;
        this.change = change;
    }

}

The InventoryManager

Which declares the delegate, event and states when to trigger the event. The client side has the same class with the only difference being the UpdateInventory content, which is null...everything else is the same.

public class InventoryManager:MarshalByRefObject
{
    public delegate string InventoryChangeHandler(object sender, InventoryChangeArgs info);
    public event InventoryChangeHandler Changed;
    public void UpdateInventory(string pno, int change)
    {
        Console.WriteLine("UpdateInveroty has been invoced");
        if (change == 0)
            Console.WriteLine("No changed event");
        else if (Changed != null)
        {
            Changed(this, new InventoryChangeArgs(pno, change));
            Console.WriteLine("Changed event");
        }
    }
}

Event's function on the client side Adds to the event the Wan function and sets the Remoting Configuration:

class Client
{
    static void Warn(object sender,InventoryChangeArgs args)
    {
        Console.WriteLine("It was changed by: {0] the {1}",args.Change,args.Pno);
    }
    static void Main(string[] args)
    {
        RemotingConfiguration.Configure("Client.exe.config",false);
        InventoryManager inv=new InventoryManager();
        inv.Changed+=new InventoryManager.InventoryChangeHandler(Warn);
        Console.WriteLine("Client started");
        inv.UpdateInventory("102", 2);
        Console.ReadLine();
    }
}

Remote configuration file This the remote configuration file for the client, the server is almost the same with the exception of the following lines:

...
<application name="Server">
      <service>
        <wellknown
          mode="Singleton"
          type="InventoryManager, InventoryManager"
          objectUri="InventoryManager" />
      </service>
    ...
<channel ref="tcp" port="9000">

...

<configuration>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknown
          type="InventoryManager, InventoryManager"
          url="tcp://localhost:9000/Server/InventoryManager"
        />
      </client>
      <channels>
        <channel ref="tcp" port="0">
          <serverProviders>
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>
        </channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

If anyone could give me some assistance I would be much appreciated. Thanks in advance

来源:https://stackoverflow.com/questions/5330633/c-sharp-remote-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!