Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have web service and a web method that has System.IO.Stream return type.
C#
[WebMethod]
public System.IO.Stream GetDYFtpWebRequestStream(string firmName, string filePath)
{
    string ftpAdress = ftpServer + ":" + ftpPort + "/" + ftpFolder + "/" + firmName + "/" + Path.GetFileName(filePath);
    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpAdress);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential(ftpUser, ftpPass);
    request.UsePassive = true;
    request.UseBinary = true;
    request.KeepAlive = true;
    return request.GetRequestStream();
}

I want to use this method in my Windos Service app like this:
C#
using (var fs = File.OpenRead(filePath))

    {
    
        System.IO.Stream ftpStream = Licence.LicenceServiceClient.GetDYFtpWebRequestStream(AppSettings.firmCode, filePath);
        fs.CopyTo(ftpStream);
        ftpStream.Close();
        status = "Dosya başarıyla " + ftpAdress + " adresine yüklendi!";
        return status;
    
    }

When I try use this method, I got this error:
Cannot implicitly convert type 'DBBackupWinService.LicenceService.Stream' to 'System.IO.Stream'

How can I fix this? My purpose is to keep FTP credentials on the web service, not in my application. Does this make sense? What should I do ?

Thanks.

What I have tried:

I tried the suggestion below but I am still getting same error.

C#
[WebMethod]
        public System.IO.Stream GetDYFtpWebRequestStream(string firmName, string filePath)
        {
                System.IO.Stream newStream = new System.IO.MemoryStream();
                string ftpAdress = ftpServer + ":" + ftpPort + "//" + ftpFolder + "/" + firmName + "/" + Path.GetFileName(filePath);
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpAdress);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpUser, ftpPass);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = true;
                newStream = (System.IO.Stream)request.GetRequestStream();
                return newStream;
        }
Posted
Updated 14-Dec-20 22:06pm

1 solution

You cannot return a stream from a web method - not even a read-only stream, let alone a writeable stream.

You will need to read the file contents in the client and pass it in to the web method.
C#
[WebMethod]
public void UploadFileToFtp(string firmName, string filePath, byte[] fileBytes)
{
    string ftpAdress = ftpServer + ":" + ftpPort + "//" + ftpFolder + "/" + firmName + "/" + Path.GetFileName(filePath);
    using (FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpAdress))
    {
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(ftpUser, ftpPass);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = true;
        using (var stream = request.GetRequestStream())
        {
            stream.Write(fileBytes);
        }
    }
}
C#
byte[] fileBytes = File.ReadAllBytes(filePath);
Licence.LicenceServiceClient.UploadFileToFtp(AppSettings.firmCode, filePath, fileBytes);
status = "Dosya başarıyla " + ftpAdress + " adresine yüklendi!";
 
Share this answer
 
Comments
Member 11382409 15-Dec-20 16:09pm    
Thanks for your suggestion. I tried it. If I use byte[] parameter type, when I call UploadFileToFtp method in my application "System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://localhost:63702" error occurs. After this error I removed byte[] parameter for test, method worked fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900