2010-02-13 98 views
0

我在我的代碼中使用WCF服務,客戶端(WindowsFormsApplication1)捕獲桌面視圖並將其發送到服務器。之後服務器將圖像發送到Masterclient(windowsformsApplication2)。其工作...但幾分鐘,我得到了來自客戶方的例外,因爲對象引用不是設置對象我怎樣才能解決這個問題的一個實例....WCF客戶端中的異常

,這是mycode的:

public void SendToServerToMainServer(clsImageObject img) 
{ 

     ConnectToServerSettings(); 
     InterfaceClass.IService serviceobj = Client.CreateChannel();// I got exception in This line,And the serviceobj got null Suddenly... 
     serviceobj.SendToServerToMasterClient(img, clpro.MyIPAddress); 
     Client.Close(); 
     Client = null; 
    } 
    } 




public void ConnectToServerSettings() 
{ 

     string StrAddress = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "url1.txt"); 
     //EndpointAddress ea = new EndpointAddress(@"net.tcp://10.0.3.33:2222/ClsPCMain"); 
     EndpointAddress ea = new EndpointAddress(StrAddress); 
     NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false); 
     //binding.TransferMode = TransferMode.Streamed; 
     binding.MaxBufferPoolSize = Int32.MaxValue; 
     binding.MaxReceivedMessageSize = Int32.MaxValue; 
     binding.PortSharingEnabled = true; 
     binding.ReceiveTimeout = TimeSpan.MaxValue; 
     binding.SendTimeout = TimeSpan.MaxValue; 
     binding.OpenTimeout = TimeSpan.MaxValue; 
     binding.CloseTimeout = TimeSpan.MaxValue; 
     binding.MaxReceivedMessageSize = Int32.MaxValue; 
     binding.MaxBufferPoolSize = Int32.MaxValue; 
     binding.MaxConnections = Int16.MaxValue; 
     binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue; 
     binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue; 
     binding.ReaderQuotas.MaxDepth = Int32.MaxValue; 
     binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue; 
     binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; 
     binding.Security.Mode = SecurityMode.None; 
     Client = new ChannelFactory<InterfaceClass.IService>(binding, ea); 

    } 

} 
+0

你能否提供一些關於異常的地方的信息(即什麼實際上是空的)? – 2010-02-13 13:09:09

+0

我已經在註釋行中的代碼中給出了它...它是在第一個函數SendToServerToMainServer(clsImageObject img)... – Suryakavitha 2010-02-13 14:04:10

+0

serviceobj對象獲取null .. – Suryakavitha 2010-02-15 05:42:22

回答

0

試使用後關閉您的頻道

 
using (InterfaceClass.IService serviceobj = Client.CreateChannel()) 
{ 
    serviceobj.SendToServerToMasterClient(img, clpro.MyIPAddress); 
    serviceobj.Close(); 
    Client.Close(); 
    Client = null; 
} 

tbh,我不確定此代碼是否乾淨。您似乎正在使用您填寫1方法的類(實例?)變量'客戶端',並關閉其他清除的&。沒有太多的代碼重寫,我會修改'ConnectToServerSettings()'的簽名以返回客戶端實例,而不是將其推送到變量中。

希望這有助於

+0

我可以在代碼中多次使用它嗎?這意味着我應該如何使用這個?,我曾多次使用過serviceobj變量,我應該每次在每次使用serviceobj變量時都給這些代碼。 – Suryakavitha 2010-02-13 14:09:32

+0

當然,您可以在使用您當前的代碼的地方使用它。主要區別在於我們在使用後不久關閉頻道。不僅僅是渠道工廠。 – 2010-02-14 10:24:20

0

,你應該非常小心地關閉通道,並把對象和null檢查整個應用程序,以避免對象引用空例外。