远程事件问题[C#] [英] Remote Events Issue[C#]

查看:82
本文介绍了远程事件问题[C#]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用远程事件从服务器调用客户端上存在的回调方法。但是我遇到了这个我不明白的问题...我要把我的代码放在这里和堆栈跟踪...希望有人可以用它做出一些东西......:
如果你觉得我我的道歉,我已经解释了我的问题,而不是粘贴我的代码。只是因为我无法用正确的话说出来......对不起......希望有人可以帮助我...

共享装配
一般性用户:


 public delegate void EventHandling(string msg); 

公共类RemoteEvents:MarshalByRefObject
{
public void MyMethod(string msg)
{
try
{
if(
if( myActualHandler!= null)//此处没有问题。
myActualHandler(msg); //问题!我不知道为什么...
else
Console.WriteLine("问题注册事件");
}
catch(异常e)//此处捕获异常。
{
MessageBox.Show(e.StackTrace.ToString());

}
}

公共事件EventHandling myActualHandler;

公共覆盖对象InitializeLifetimeService()
{
//此对象必须生存"永远"
返回null;
}
}


//事件包装器类,用于为服务器和客户端提供服务器调用的方法的共享//定义。
public class EventWrapper:MarshalByRefObject
{
public event EventHandling myLocalHandler;

//控制永远不会到达这个地方。
[OneWay]
public void MyLocalHandlerMethod(string msg)
{

try
{
if(myLocalHandler!= null)
myLocalHandler(msg);
else
Console.WriteLine("问题注册myLocalHandler事件");
}
catch(例外e)
{
Console.WriteLine("问题与MyLocalHandlerMethod !!!");
Console.WriteLine(" {0}",e.StackTrace);
}
}
公共覆盖对象InitializeLifetimeService()
{
//此对象必须生存"永远"
返回null;
}
}




客户:


 public partial class Form1:Form 
{
RemoteEvents remObj;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender,EventArgs e)
{

//在这里创建远程对象:
remObj = (RemoteEvents)Activator.GetObject(typeof(General.RemoteEvents)," tcp:// server:30300 / RemoteEvents.rem");
EventWrapper eventWrapper = new EventWrapper();

eventWrapper.myLocalHandler + = new EventHandling(myHandlerMethod);

remObj.myActualHandler + = new EventHandling(eventWrapper.MyLocalHandlerMethod);
}


//服务器引发事件时要调用的方法。
public void myHandlerMethod(String msg)
{
MessageBox.Show(msg);
}


//这是在服务器上引发事件然后回调上面的方法
private void button1_Click(object sender,EventArgs e )
{
remObj.MyMethod("Remote Event Handled !!!");
}
}




服务器:
服务器是控制台应用程序。重要的是这个:


 BinaryServerFormatterSinkProvider formatterBin = 
new BinaryServerFormatterSinkProvider();

formatterBin.TypeFilterLevel = TypeFilterLevel.Full;



我将TypeFilterLevel设置为FULL,允许回调功能。


堆栈跟踪错误:<在System.Runtime.Remoting.Proxies.RemotingProxy.InternalInvoke(IMethodCallMessage reqMcmMsg,Boolean useDispatchMessage,Int32 callType)
at System.Runtime.Remoting.Proxies,

 .RemotingProxy.Invoke(IMessage reqMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 type)
at General.EventWrapper.MyLocalHandlerMethod(String msg)
在General.RemoteEvents.MyMethod(String msg)



解决方案

你可以包括异常以及微量

I am trying to invoke a callback method present on my client from the server using remote events. but i am running into this problem which i dont understand... I am going to put my code here and the stack trace... hope someone can make something out of it...:
If you feel that i sould have explained my problem instead of pasting my code, my apologies. Its just that i am not able to put it in right words... sorry... hope someone can help me...

Shared assembly
General.dll:

    public delegate void EventHandling(string msg);

    public class RemoteEvents: MarshalByRefObject
    {
        public void MyMethod(string msg)
        {
             try
            {
                if (myActualHandler != null)//No issues here.
                    myActualHandler(msg);// PROBLEM! i dont know why...
                else
                    Console.WriteLine("Problem Registering event");
            }
            catch (Exception e)// the exception is caught here.
            {
                MessageBox.Show(e.StackTrace.ToString());
               
            }
        }

        public event EventHandling myActualHandler;

        public override object InitializeLifetimeService()
        {
            // this object has to live "forever"
            return null;
        }
    }


// Event wrapper class used to provide the server and the client with a shared //definition of the method to be called by the server.
    public class EventWrapper : MarshalByRefObject
    {
        public event EventHandling myLocalHandler;

// Control never reaches this place.
        [OneWay]
        public void MyLocalHandlerMethod(string msg)
        {
           
            try
            {
                if(myLocalHandler != null)
                    myLocalHandler(msg);
                else
                    Console.WriteLine("Problem Registering myLocalHandler event");
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem with MyLocalHandlerMethod!!!");
                Console.WriteLine("{0}", e.StackTrace);
            }
        }
        public override object InitializeLifetimeService()
        {
            // this object has to live "forever"
            return null;
        }
    }




Client:

public partial class Form1 : Form
    {
        RemoteEvents remObj;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

// Creating the remote Object here:
            remObj =  (RemoteEvents)Activator.GetObject(typeof(General.RemoteEvents), "tcp://server:30300/RemoteEvents.rem");
            EventWrapper eventWrapper = new EventWrapper();

            eventWrapper.myLocalHandler += new EventHandling(myHandlerMethod);

            remObj.myActualHandler += new EventHandling(eventWrapper.MyLocalHandlerMethod);
        }


// the method to be called when the event is raised by the server.
        public void myHandlerMethod(String msg)
        {
            MessageBox.Show(msg);
        }


// this is to raise an event at the server which then calls back the above //method
        private void button1_Click(object sender, EventArgs e)
        {
            remObj.MyMethod("Remote Event Handled!!!");
        }
    }




Server:
the server is a console app. the important part is this:

           

BinaryServerFormatterSinkProvider formatterBin =  
                                            new  BinaryServerFormatterSinkProvider();

formatterBin.TypeFilterLevel = TypeFilterLevel.Full;


I am setting the TypeFilterLevel to FULL which allows callback functionality.


Stack Trace of the error:
  

at System.Runtime.Remoting.Proxies.RemotingProxy.InternalInvoke(IMethodCallMessage reqMcmMsg, Boolean useDispatchMessage, Int32 callType)
   at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(IMessage reqMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at General.EventWrapper.MyLocalHandlerMethod(String msg)
   at General.RemoteEvents.MyMethod(String msg)


解决方案

Could you include the exception along with the trace?


这篇关于远程事件问题[C#]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆