Home » .Net Framework

Copy Files to FTP Server using WCF

Dear All,

I have a ftp server in my desktop computer and I want to copy files to FTP server using Windows Communication Foundation. Below is part of my code:






string OperationUploadData();


publicclassUploadServiceClass : IUploadService


publicstring OperationUploadData()




FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://pasco:22/");

request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = newNetworkCredential("administrator", "password");

StreamReader sourceStream = newStreamReader("test.txt");

byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());


request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();

requestStream.Write(fileContents, 0, fileContents.Length);


FtpWebResponse response = (FtpWebResponse)request.GetResponse();


return"Upload File Complete, status " + response.StatusDescription;


catch (Exception ex)


return ex.Message.ToString();





ServiceHost sh = null;

Uri tcpa = newUri("net.tcp://localhost:8000/TcpBinding");

sh = newServiceHost(typeof(UploadServiceClass), tcpa);

NetTcpBinding tcpb = newNetTcpBinding();

ServiceMetadataBehavior mBehave = newServiceMetadataBehavior();


sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

sh.AddServiceEndpoint(typeof(IUploadService), tcpb, tcpa);


Client Application:

TCP.UploadServiceClient client = new ClientApplication.TCP.UploadServiceClient("NetTcpBinding_IUploadService");

byte[] a = newbyte[10];

for (int i = 0; i < 10; i++)


aIdea = (byte)i;


string message = client.OperationUploadData();



Service.cs provide the contract and ServiceHost.cs provides address and bindings. When I run client application there was an error message : "The requested URI is invalid for this FTP command." I do not know how to connect FTP using WCF. I really need help as soon as possible. Thank you.


5 Answers Found


Answer 1

You shouldn't have any problems opening a FTP connection from within a WCF service.

What happens if, on the client, you actually create an instance of the service  and call the OperationUploadData directly (i.e., without WCF in the middle)?

UploadServiceClass usc = new UploadServiceClass();



Answer 2

Sorry, I just test my code  without WCF and the error message  still same. I begin think that this error  not cause by WCF. I will try to change my code first so the program can running well and try to use WCF in the middle again. I will give news if the program can running well. Thanks for your help.

Best Regards,



Answer 3

My problem has solved. I added my code  like this

FtpWebRequest request  = (FtpWebRequest)WebRequest.Create("ftp://pasco:22/" + FileName);

And now the program has running well.

Thanks again for any help that gives to me.

Best Regards,



Answer 4


  My Vb.net coding is

Dim result As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

Using rs As Stream = ftp.GetRequestStream

ftp.getrequeststrem    Generate error  

Error message  is

The requested  URI is invalid  for this ftp  command.

How i solve this error , i need urgent soluction please help me




Answer 5

HI ,

I am working on exact same requirement and this is my first experience with WCF. can you please provide me some steps as to create this service. i have hard time understanding what code  goes where and how to install and test this application.. can you please take some time to gives the steps in visual studio 2008 to get this working.

thanks much for your help.





<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure