4

How do I create a WebService in C# that will accept File and then return a File at the same time in one call (Synchronous). What I'm trying to do is to create a WebService that will accept and MS Office document, convert that document to PDF and then return that file back to the caller (in my case I'm using Java for the client)

Maksim
  • 16,635
  • 27
  • 94
  • 135
  • 1
    You could create a Wcf Service convert the file to bytes, send it as byte array and make the return type also a byte array. You probably need to increase the send timeout since the default is 1 minute. Normally you could use a stream but I remembered it to have some downside in parameters. – Silvermind Mar 28 '12 at 17:21
  • 1
    As Silvermind says. You can look for WCF and the POST verb. The PDF processing is a server side business only. Your WCF method need to return a object to client. Not sure how HttpResponse.Stream apply to a WCF method? Or else, just return a url string where a processed pdf file will be created (or stored), which you redirect the client to. – Independent Mar 28 '12 at 17:30

4 Answers4

2

As silvermind said in his comment, the best option is to accept and return an array of bytes in your webservice.

You can load a file as a bytearray with a method like this one:

public byte[] FileToByteArray(string _FileName)
{
    byte[] _Buffer = null;

    try
    {
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
        long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
        _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
        _FileStream.Close();
        _FileStream.Dispose();
        _BinaryReader.Close();
    }
    catch (Exception _Exception)
    {
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }
    return _Buffer;
}

In addition, if you have implemented the webservice as a WCF service, you may need to tweak some settings to increase the ammount of information you can send and the timeout. This is sample of the binding configuration that allow that. (Only a sample, may not match your needs)

 <binding name="WebServiceBinding" closeTimeout="00:02:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
Jonathan
  • 11,809
  • 5
  • 57
  • 91
0

you can encode small amounts of binary data into a base64 string.

mo.
  • 3,474
  • 1
  • 23
  • 20
0

here are couple of tutorials from different sources it also depends on what you are using wcf or asmx. I also think you have to create two separate function and another function that will call send and revive simultaneously although you might want to have some time out for a send to take place before you can receive it.

http://support.microsoft.com/kb/318425

http://www.zdnetasia.com/create-a-simple-file-transfer-web-service-with-net-39251815.htm

https://stackoverflow.com/questions/4530045/how-to-transfer-file-through-web-service

Community
  • 1
  • 1
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • why have not used it in a while?? – COLD TOLD Mar 28 '12 at 17:37
  • It's old technology and WCF give you a lot of tools to meet modern web technology/security. There are ton of blog posts and studies on net regarding this. The good left of asmx is that the services are shame easy to setup. – Independent Mar 28 '12 at 17:42
  • I totally agree but sometimes people are limited to specific framework. – COLD TOLD Mar 28 '12 at 17:43
  • Thats one of the reasons. Asmx *force* all part to be of MS type. WCF SOAP can talk with other type of systems. If i'm not wrong, wcf was a addon to .net 2, builtin since 3.0? Sorry for my typos, im on mobile. – Independent Mar 28 '12 at 17:48
0

The easiest way is to integrate the basic librarys of the ASP.net MVC3 framework into your basic Webproject and then write a simple MVC Controller with a single method which returns an object of type FileResult.

Scott Hanselman has published a great blog post about doing that in minutes: http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx

It works perfectly fine and is done in less then 3minutes (after MVC3 framework integration). There is already a stackoverflow post about the returning of the file in MVC: How to create file and return it via FileResult in ASP.NET MVC?

Greetings,

Community
  • 1
  • 1
Mario Fraiß
  • 1,095
  • 7
  • 15