类型'System.Threading.Tasks.Task'无法序列化

时间:2013-09-27 07:09:28

标签: c# wcf service web

我在WCF中创建了一个自托管的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);
                        }
                    }

1 个答案:

答案 0 :(得分:0)

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