简体   繁体   中英

Exception in WCF client

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

    }

}

try closing your channel after use

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

tbh, i'm not sure this code is clean. You seem to be using a class (instance?) variable 'Client' which you fill in 1 method, and close&clear in other. Without to much code rewrite, i would modify the signature of the 'ConnectToServerSettings()' to return the Client instance instead of pushing it in a variable.

Hope this helps,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM