WCF客户端中的异常

时间:2010-02-13 12:58:21

标签: c# wcf wcf-client

我在我的代码中使用了WCF服务,客户端(WindowsFormsApplication1)捕获桌面视图并将其发送到服务器。之后,服务器将图像发送到Masterclient(windowsformsApplication2)。它工作......但是几分钟我从clientSide获得异常,因为对象引用不是设置对象的实例我如何解决这个问题....

这是我的代码:

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);

    }

}

2 个答案:

答案 0 :(得分:0)

尝试在使用后关闭频道

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

tbh,我不确定这段代码是否干净。您似乎使用了一个类(实例?)变量'Client',您填写1方法,并在其他方法中关闭&amp;清除。如果没有太多代码重写,我会修改'ConnectToServerSettings()'的签名以返回Client实例,而不是将其推送到变量中。

希望这有帮助,

答案 1 :(得分:0)

您应该非常小心地关闭通道,并在整个应用程序中放置对象空值检查,以避免对象引用为空的异常。