我有一個.NET遠程處理客戶端激活對象類型客戶端服務器應用程序。我正在使用面向.NET Framework 4的Visual Studio 2010。當我在沒有調試的情況下運行時,程序工作正常 - 客戶端可以連接沒有問題。但是當我嘗試調試時,在客戶端代碼中有'new'操作符的行中,服務器(我認爲)會引發異常。工作.net遠程處理項目調試時拋出System.Reflection.TargetInvocationException
我想要做的是保持對在服務器代碼中創建的遠程對象的引用。所以在我的遠程對象的構造函數中我有這樣一行:
Cache.GetInstance().addFireFighter(this);
當我調試,運行此代碼也沒關係。但是當它返回到遠程對象的行來調用addFireFighter
方法時,就是它崩潰的時候。下面是addFireFighter
方法:
public static IServer _server;
public void addFireFighter(FireFighter ff)
{
_server.addFireFighter(ff);
}
而且_server.addFireFighter
方法:
public void addFireFighter(FireFighterResponder.FireFighter ff)
{
_ffList.Add(ff); // -> works fine :S
Console.WriteLine("FireFighterResponder addFireFighter added");
lstBox.Items.Add(ff); //-> CRASH!!
}
觀察:當我運行無需調試服務器,但運行在調試模式下的客戶端,它仍然能正常工作。
這是一個學校項目,我是C#.NET遠程處理新手。我在Java中實現了這一點,並沒有問題。所以我可以給我的整個項目,如果有人想看看它。也許我有一個設計缺陷。
這裏的堆棧跟蹤:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Cross-thread operation not valid: Control 'lstBox' accessed from a thread other than the thread it was created on. at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.ListBox.NativeAdd(Object item) at System.Windows.Forms.ListBox.ObjectCollection.AddInternal(Object item) at System.Windows.Forms.ListBox.ObjectCollection.Add(Object item) at FirefighterMonitorSystem.BaseStation.addFireFighter(FireFighter ff) in C:\Users\Dula\Documents\My Dropbox\Firefighter\453\FirefighterMonitorSystem\FirefighterMonitorSystem\BaseStation.cs:line 35 at FireFighterResponder.Cache.addFireFighter(FireFighter ff) in C:\Users\Dula\Documents\My Dropbox\Firefighter\453\FirefighterMonitorSystem\FireFighterResponder\Cache.cs:line 33 at FireFighterResponder.FireFighter..ctor() in C:\Users\Dula\Documents\My Dropbox\Firefighter\453\FirefighterMonitorSystem\FireFighterResponder\FireFighter.cs:line 20 --- End of inner exception stack trace ---
的lstBox
是一個.NET ListBox
在我的服務器代碼,我添加的每個遠程對象。但是我仍然困惑的是爲什麼它不在調試模式時工作。
很遺憾你的學校落後於時代。 .NET Remoting已被棄用,以支持WCF。 – 2011-03-29 18:51:15