2010-02-13 67 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检查整个应用程序,以避免对象引用空例外。