问题
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)
回答1:
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>
回答2:
you can encode small amounts of binary data into a base64 string.
回答3:
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
回答4:
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,
来源:https://stackoverflow.com/questions/9912495/accept-and-return-file-to-from-c-sharp-webservice