Home » .Net FrameworkRSS

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:

WCFService.cs

[ServiceContract]

publicinterfaceIUploadService

{

[OperationContract]

string OperationUploadData();

}

publicclassUploadServiceClass : IUploadService

{

publicstring OperationUploadData()

{

try

{

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());

sourceStream.Close();

request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();

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

requestStream.Close();

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

response.Close();

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

}

catch (Exception ex)

{

return ex.Message.ToString();

}

}

}

WCFServiceHost:

ServiceHost sh = null;

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

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

NetTcpBinding tcpb = newNetTcpBinding();

ServiceMetadataBehavior mBehave = newServiceMetadataBehavior();

sh.Description.Behaviors.Add(mBehave);

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

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

sh.Open();

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();

Console.WriteLine(message);

Console.ReadLine();

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();

Console.WriteLine(usc.OperationUploadData());

 

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,

LIT

 

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,

LIT

 

Answer 4

  Hai

  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

Regards

Arun

 

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.

-Naresh

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter