2010-02-13 2 views
0

클라이언트 (WindowsFormsApplication1)가 데스크톱보기를 캡처하여 서버로 보내는 코드에 WCF 서비스를 사용하고 있습니다. 서버가 Masterclient (windowsformsApplication2)로 이미지를 보냅니다. 하지만 객체 참조 내가이 문제를 해결할 수있는 방법은 객체의 인스턴스를 설정하는 아니므로 몇 분 전 ....WCF 클라이언트의 예외

를 클라이언트 측에서 예외를 가지고 이것은 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); 

    } 

} 
+0

예외가 발생한 위치에 대한 정보를 추가로 표시 할 수 있습니까? –

+0

나는 이미 주석 줄에서 그 코드를주었습니다 ... 첫 번째 함수 인 SendToServerToMainServer (clsImageObject img) ... – Suryakavitha

+0

serviceobj 객체가 널이됩니다 .. – Suryakavitha

답변

0

시도 사용 후 채널 닫기

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

tbh,이 코드가 깨끗한 지 확실하지 않습니다. 당신은 하나의 메소드를 채우는 클래스 (인스턴스?) 변수 'Client'를 사용하고있는 것으로 보이며, &을 다른 메소드로 닫습니다. 많은 코드를 다시 작성하지 않고 'ConnectToServerSettings()'의 시그니처를 수정하여 Client 인스턴스를 변수로 푸시하는 대신 반환합니다. 당신은 매우 신중하게 채널을 닫고 객체 참조는 null 예외를 방지하기 위해 응용 프로그램 전체에서 객체는 null 검사를 넣어해야

+0

코드 내부에서 여러 번 사용할 수 있습니까? 즉, 어떻게 사용해야합니까?, 나는 serviceobj 변수를 여러 번 사용했습니다. serviceobj 변수를 사용할 때마다이 코드를 매번 제공해야합니다. – Suryakavitha

+0

물론 현재 코드를 사용하는 곳에서이 코드를 사용할 수 있습니다. 주요 차이점은 사용 직후에 채널을 폐쇄한다는 것입니다. 채널 공장 만이 아닙니다. –

0

이 도움이

희망.