开发者

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.SendToSe开发者_运维百科rverToMasterClient(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,


you should close the channel very carefully and put object null checks throughout the application to avoid object reference null exceptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜