2012-04-22 2 views
0

임 [의 WinForm]소켓 엔드 포인트를 WCF 서비스에 전달하는 방법은 무엇입니까? WCF에서 클라이언트 측의 파일</p> <p>를 전송하는 WCF를 사용

private void Send(TransferEntry Entry, int bufferSize) 
    { 
     using (FileStream fs = new FileStream(Entry.SrcPath, FileMode.Open, FileAccess.Read)) 
     { 
      byte[] buffer = new byte[bufferSize]; 
      long sum = 0; 
      int count = 0; 
      using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) 
      { 
       IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0); 
       listener.Bind(endpoint); 
       listener.Listen(1); 
       client.Receive(listener.LocalEndPoint, Entry.DestPath, Entry.Size, bufferSize); 
       //i get an exception here. 
       using (Socket socket = listener.Accept()) 
       { 
        do 
        { 
         count = fs.Read(buffer, 0, buffer.Length); 
         socket.Send(buffer, 0, count, SocketFlags.None); 
         sum += count; 
         worker.ReportProgress((int)((sum * 100)/Entry.Size)); 
        } while (sum < Entry.Size); 
       } 
      } 
     } 
    } 

[서비스] IFileManager.cs에 : FileManagerService.cs에

[OperationContract] 
    void Receive(System.Net.EndPoint endpoint, string destPath, long size, int bufferSize); 

:

public void Receive(EndPoint endpoint, string destPath, long size, int bufferSize) 
    { 
     using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) 
     { 
      client.Connect(endpoint); 
      using (FileStream fs = new FileStream(destPath, FileMode.Create, FileAccess.Write)) 
      { 
       byte[] buffer = new byte[bufferSize]; 
       long sum = 0; 
       int count = 0; 
       do 
       { 
        count = client.Receive(buffer, 0, buffer.Length, SocketFlags.None); 
        fs.Write(buffer, 0, count); 
       } while (sum < size); 
       fs.Flush(); 
      } 
     } 
    } 

는 내가 클라이언트 측 [의 WinForm]에이 예외를 가지고 :

System.ServiceModel.CommunicationException was unhandled by user code Message=There was an error while trying to serialize parameter http://tempuri.org/:endpoint. The InnerException message was 'Type 'System.Net.IPEndPoint' with data contract name 'IPEndPoint:http://schemas.datacontract.org/2004/07/System.Net' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

왜 내가 EndPoint 매개 변수를 전달하면이에만 발생한다! 어떻게 작동시킬 수 있습니까?

답변

0

서버는 EndPoint를 기대하고 있지만, 당신은 IPEndPoint.

당신은 ServiceKnownTypeAttribute를 사용하여 IPEndPoint을 기대하는 서비스를 말함으로써이 문제를 해결할 수 있습니다 보내고있다.