1

我在 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);
                        }
                    }
4

1 回答 1

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);
}
于 2015-02-03T14:48:17.167 回答