0

i have using WCF service in my code that the client(WindowsFormsApplication1) capturing desktop view and send it to Server .. After that the Server will send the images to Masterclient(windowsformsApplication2).Its working... but few minutes i got the exception from clientSide as object reference is not to set an instance of an object How can i solve this problem....

and this is 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);

    }

}
4

2 回答 2

0

尝试使用后关闭您的频道

使用 (InterfaceClass.IService serviceobj = Client.CreateChannel())
{
    serviceobj.SendToServerToMasterClient(img, clpro.MyIPAddress);
    服务对象。关闭();
    Client.Close();
    客户=空;
}

tbh,我不确定这段代码是否干净。您似乎正在使用一个类(实例?)变量“客户端”,您填写 1 个方法,并在其他方法中关闭和清除。无需大量代码重写,我将修改“ConnectToServerSettings()”的签名以返回客户端实例,而不是将其推送到变量中。

希望这可以帮助,

于 2010-02-13T13:29:42.473 回答
0

您应该非常小心地关闭通道并在整个应用程序中进行对象空检查以避免对象引用空异常。

于 2010-04-01T11:05:17.193 回答