C# – WebService parameter object being renamed

cweb services

This is the first time I've used a webservice for anything so the question may be a little basic. Anyhow, I have a webservice that acts as a proxy to our vendors site. It simplifies the "screen scrape" that we would usually have to do. The webservice function looks like this:

namespace foo  
{  
  public class MyService : WebService  
  {
     [WebMethod]  
     public string UploadFile(System.IO.FileStream fileToUpload)  
     {
        return _obj.Upload(fileToUpload);  
     }  
  }  
}    

The client throws an error when you try to give it the FileStream that the method asks for. In compilation somewhere, the webservice changed the type of the parameter from System.IO.FileStream to foo.FileStream. Does anyone have any ideas as to how I did this to myself?

Thanks in advance!

Best Answer

In .NET when you are making calls across application domains (as you are here), you can't pass data that is specific to that application domain.

The general version of this is that when you are making calls between two separate processes, you can't send information that is specific (i.e. only has significance in that context) to that process and expect it to have significance in the other process.

This is what you are doing with the filestream. The filestream is a handle to the file on the OS that is specific to a process. There is no guarantee that a process on the same computer, let alone a process on another machine will be able to understand this.

This being a web service, that's exactly the situation you have, as you have two processes on different machines.

To address the issue, the data you send has to be self-contained. In this specific case, that means sending the contents of the entire file.

You should change the parameter to a byte array, and then process the bytes appropriately in your method.