簡體   English   中英

類型'System.Threading.Tasks.Task'無法序列化

[英]Type 'System.Threading.Tasks.Task' cannot be serialized

我在WCF中創建了一個自托管的Web服務。 當我嘗試從不同的系統調用此Web服務時,會導致不同的異常,並且在某些地方運行良好。

我的服務如下。

網絡服務:

namespace KryptonWebService
{      

        [ServiceContract()]
        public interface ISetUpHost
        {
            [OperationContract()]
            void EditHostFile(bool flag = true);
        }




        [ServiceContract]
        public interface IFileTransferService
        {
            [OperationContract]//(IsOneWay = true)
           // void UploadFile(FileUploadMessage request);
            void UploadFile(Stream FileByteStream);
            [OperationContract] //(IsOneWay = false)
            Stream DownloadFile();
        }
}

實施類:

public class KryptonService : KryptonWebService.ISetUpHost, KryptonWebService.IFileTransferService
    {
        //public int Add(int num1, int num2)
        //{
        //    return num1 + num2;
        //}

        public void EditHostFile(bool flag = true)
        {
            Console.WriteLine("Do Nothing");
        }


        //public void UploadFile(FileUploadMessage request)
        //{


        //    string basePath = @"C:\Users\Mahesh\Downloads\UploadedZip\";
        //    string serverFileName = Path.Combine(basePath, request.Filename);

        //    using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
        //    {
        //        const int bufferSize = 65536; // 64K

        //        Byte[] buffer = new Byte[bufferSize];
        //        int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);

        //        while (bytesRead > 0)
        //        {
        //            outfile.Write(buffer, 0, bytesRead);
        //            bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
        //        }
        //    }

        //}

        //public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
        //{
        //    string localFileName = request.Filename;
        //    try
        //    {
        //        string basePath = @"C:\Users\Mahesh\Downloads\Zip\";
        //        string serverFileName = Path.Combine(basePath, request.Filename);

        //        Stream fs = new FileStream(serverFileName, FileMode.Open);

        //        return new FileDownloadReturnMessage(request.Filename, fs);
        //    }
        //    catch (IOException e)
        //    {
        //       return null;

        //    }            
        //}


        public void UploadFile(Stream FileByteStream)
        {
            try
            {
                string filename = "Krypton_Uploaded.zip";
                string basePath = @"C:\Users\Mahesh\Downloads\UploadedZip\";
                string serverFileName = Path.Combine(basePath, filename);

                using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
                {
                    const int bufferSize = 65536; // 64K

                    Byte[] buffer = new Byte[bufferSize];
                    int bytesRead = FileByteStream.Read(buffer, 0, bufferSize);

                    while (bytesRead > 0)
                    {
                        outfile.Write(buffer, 0, bytesRead);
                        bytesRead = FileByteStream.Read(buffer, 0, bufferSize);
                    }
                }
            }
            catch (IOException e)
            {
                //    throw new FaultException<ioexception>(e);
            }
        }

        public Stream DownloadFile()
        {
            string localFileName = "Krypton1.zip";
            try
            {
                string basePath = @"C:\Users\Mahesh\Downloads\Zip\";
                string serverFileName = Path.Combine(basePath, localFileName);

                Stream fs = new FileStream(serverFileName, FileMode.Open);

              //  return new KryptonWebService.FileDownloadReturnMessage(fs);

                return fs;
            }
            catch (IOException e)
            {
                string msg = e.Message;
                return null;
            }
        }
}

現在在客戶端上:

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IFileTransferService" maxReceivedMessageSize="32423432" />
        <binding name="BasicHttpBinding_ISetUpHost" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://10.101.23.91:8090/KryptonService/Krypton"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransferService"
          contract="IFileTransferService" name="BasicHttpBinding_IFileTransferService" />
      <endpoint address="http://10.101.23.91:8090/KryptonService/Krypton"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISetUpHost"
          contract="ISetUpHost" name="BasicHttpBinding_ISetUpHost" />
    </client>
  </system.serviceModel>
</configuration>

客戶代碼:

       private static void UploadFile(string uploadpath)
            {
                FileTransferServiceClient fileTransferClient = new FileTransferServiceClient();
                string uploadfilename = "Krypton.zip";


                //  using (FileStream fs = new FileStream(@"C:\Users\Mahesh\Downloads\Zip\" + uploadfilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (FileStream fs = new FileStream(Path.Combine(uploadpath, uploadfilename), FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    fileTransferClient.UploadFile(fs);

                }

            }


 string downloadfilename = "KryptonDownloaded.zip";                
                Stream fileStream = null;
              //  FileDownloadReturnMessage fsd = new FileDownloadReturnMessage();

                using (FileTransferServiceClient fileTransferClient = new FileTransferServiceClient())
                {

                    try
                    {

                        fileStream = fileTransferClient.DownloadFile();


                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    Stream outputStream = null;

                    try
                    {                       

                        outputStream = new FileInfo(Path.Combine(downloadPath, downloadfilename)).OpenWrite(); //new FileInfo(@"C:\Users\Mahesh\Downloads\Downloadfrom\" + downloadfilename).OpenWrite();
                        byte[] buffer = new byte[2048];

                        int bytesRead = fileStream.Read(buffer, 0, 2048);

                        while (bytesRead > 0)
                        {
                            outputStream.Write(buffer, 0, 2048);
                            bytesRead = fileStream.Read(buffer, 0, 2048);
                        }
                    }

刪除System.Threading.Tasks.Task包含FileTransferServiceClient.cs中的方法。

例如;

public interface IFTSService
{
    [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IFTSService/ContractSend", ReplyAction = "http://tempuri.org/IFTSService/ContractSendResponse")]
    string ContractSend(SaveContractType saveContractType);

    //[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IFTSService/ContractSend", ReplyAction = "http://tempuri.org/IFTSService/ContractSendResponse")]
    //System.Threading.Tasks.Task<string> ContractSendAsync(SaveContractType saveContractType);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM