I am having problems with a delegate. I instantiate a class from my main form, and the delegate works fine. However, if I use something called xmlrpc, it is creating a new instance of the class and the delegate I made ends up null. How do I dynamically "connect" the delegate to the form?
Here is the code:
Code:
namespace RPCServerTest
{
public partial class Form1 : Form
{
InvServer srv = new InvServer(); // The class that receivies the information via xmlrpc
public Form1()
{
InitializeComponent();
}
private void subscribe()
{
srv.davdel = new InvServer.Davdel(test);
}
private void InitRPCChannel(string name, int port, string refname)
{
IDictionary props = new Hashtable();
props["name"] = name;
props["port"] = port.ToString();
HttpChannel channel = new HttpChannel(props,null,new XmlRpcServerFormatterSinkProvider());
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InvServer), refname, WellKnownObjectMode.Singleton);
}
private void Form1_Load(object sender, EventArgs e)
{
subscribe();
txtInbox.Text = "";
txtInbox.Text = "HTTP channel open." + System.Environment.NewLine;
InitRPCChannel("TestChannel",5678,"testing.rem");
}
private void test(string dt) // The delegate should give me the string I get from rpc
{
txtInbox.Text += dt + System.Environment.NewLine;
return;
}
private void cmdQuit_Click(object sender, EventArgs e)
{
txtInbox.Text += "Shutting down channel..............";
Application.DoEvents();
Application.Exit();
}
}
}
namespace RPCServerTest
{
class InvServer : MarshalByRefObject
{
public delegate void Davdel(string dat);
public Davdel davdel;
[XmlRpcMethod("testing.RPCTest")]
public string RPCTestdel(int numb)
{
string test = numb.ToString();
Onsnd(test);
return("Whatever");
}
public void Onsnd(string dt)
{
davdel(dt); // davdel is null here because xmlrpc is creating a new instance of the class
return;
}
}
}
Can I dynamically create the delegate in a constructor in the InvServer class? I am pretty sure that when I get a response over the rpc, it is creating a new instance of that class, so my delegate comes up null. How can I create it "on the fly" so to speak?
Any help would be appreciated.
Thanks