none
Copy Files to FTP Server using WCF RRS feed

  • Question

  • 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]

    public interface IUploadService

    {

    [OperationContract]

    string OperationUploadData();

    }

     

    public class UploadServiceClass : IUploadService

    {

    public string OperationUploadData()

    {

    try

    {

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

    request.Method = WebRequestMethods.Ftp.UploadFile;

    request.Credentials = new NetworkCredential("administrator", "password");

    StreamReader sourceStream = new StreamReader("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 = new Uri("net.tcp://localhost:8000/TcpBinding");

    sh = new ServiceHost(typeof(UploadServiceClass), tcpa);

    NetTcpBinding tcpb = new NetTcpBinding();

    ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();

    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 = new byte[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.

     

    Thursday, August 30, 2007 4:32 AM

Answers

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

    Thursday, August 30, 2007 7:04 AM
  • 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

    Thursday, August 30, 2007 9:03 AM

All replies

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

    Thursday, August 30, 2007 7:04 AM
  • 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

    Thursday, August 30, 2007 8:35 AM
  • 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

    Thursday, August 30, 2007 9:03 AM
  •   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

     

    Friday, September 7, 2007 9:57 AM
  • 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

     


    naresh
    Friday, August 13, 2010 10:11 PM