0

ファイルを転送するためにWCFを使用しています

クライアント側 [WINFORM]

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

WCF [サービス]: IFileManager.cs:

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

FileManagerService.cs:

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パラメータを渡したときにのみこれが発生するのはなぜですか! どうすればそれを機能させることができますか?

4

1 に答える 1

0

サーバーは を予期してEndPointいますが、送信していますIPEndPoint.

ServiceKnownTypeAttributeIPEndPointの使用を期待するようにサービスに指示することで、これを修正できます。

于 2012-04-22T15:56:08.773 に答える