C# WCF客户端中的异常
我在代码中使用了WCF服务,客户端(WindowsFormsApplication1)捕获桌面视图并将其发送到服务器。。之后,服务器将图像发送到Masterclient(windowsformsApplication2)。其工作。。。但是几分钟后,我从客户端得到了一个异常,对象引用不是设置对象的实例,如何解决这个问题 这是mycode:C# WCF客户端中的异常,c#,wcf,wcf-client,C#,Wcf,Wcf Client,我在代码中使用了WCF服务,客户端(WindowsFormsApplication1)捕获桌面视图并将其发送到服务器。。之后,服务器将图像发送到Masterclient(windowsformsApplication2)。其工作。。。但是几分钟后,我从客户端得到了一个异常,对象引用不是设置对象的实例,如何解决这个问题 这是mycode: public void SendToServerToMainServer(clsImageObject img) { ConnectToSer
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);
}
}
public void发送到服务器到服务器(clsImageObject img)
{
ConnectToServerSettings();
InterfaceClass.IService serviceobj=Client.CreateChannel();//我在这一行遇到异常,serviceobj突然变为null。。。
serviceobj.SendToServerToMasterClient(img,clpro.MyIPAddress);
Client.Close();
Client=null;
}
}
public void ConnectToServerSettings()
{
字符串StrAddress=File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+“url1.txt”);
//EndpointAddress ea=新的EndpointAddress(@“net)。tcp://10.0.3.33:2222/ClsPCMain");
EndpointAddress ea=新的EndpointAddress(StradAddress);
NetTcpBinding=新的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;
客户=新渠道工厂(绑定,ea);
}
}
使用后尝试关闭频道
using (InterfaceClass.IService serviceobj = Client.CreateChannel())
{
serviceobj.SendToServerToMasterClient(img, clpro.MyIPAddress);
serviceobj.Close();
Client.Close();
Client = null;
}
使用(InterfaceClass.IService serviceobj=Client.CreateChannel())
{
serviceobj.SendToServerToMasterClient(img,clpro.MyIPAddress);
serviceobj.Close();
Client.Close();
Client=null;
}
tbh,我不确定这个代码是否干净。您似乎正在使用一个类(实例?)变量“Client”,您在1个方法中填充它,在另一个方法中填充close&clear。如果不进行太多的代码重写,我将修改“ConnectToServerSettings()”的签名以返回客户机实例,而不是将其放入变量中
希望这会有所帮助,您应该非常小心地关闭通道,并在整个应用程序中进行对象空检查,以避免对象引用空异常。您能否提供有关异常位置的更多信息(即实际空值是什么)?我已经在注释行的代码中给出了它…它在第一个函数SendToServerToMainServer(clsImageObject img)…serviceobj对象变为null..我可以在代码中多次使用它吗?这意味着我应该如何使用这个?我多次使用serviceobj变量,我应该每次使用serviceobj变量时都给出这个代码。当然,你可以像使用当前代码时一样使用这个。主要区别在于,我们在使用后不久将关闭该频道。不仅仅是渠道工厂。