I am creating my first WCF service that uses Stream. The end product I want is to select a file from my hard disk and save it to a database. So the first step I am trying to complete is to read a selected .jpg file.
My Contract looks like this:
namespace Web { interface name "IStreamingService" [ServiceContract] public interface IStreamingService { [OperationContract] Stream GetStream(string data); } }
My IService is:
public class StreamingService : IStreamingService { public Stream GetStream(string data) { string filePath = data; try { FileStream imageFile = File.OpenRead(filePath); return imageFile; } catch (IOException ex) { Console.WriteLine( String.Format("An exception was thrown while trying to open file {0}", filePath)); Console.WriteLine("Exception is: "); Console.WriteLine(ex.ToString()); throw ex; } } }
My Web.Config file looks like this with httpRuntime maxRequestLength="2147483647" added to allow for a large file being streamed.
<!-- language: xaml -->
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!-- ADDED ASP.NET doesn’t know about WCF and it has it’s own limits for the request size, now increased to match maxReceivedMessageSize. -->
<httpRuntime maxRequestLength="2147483647"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Then I have Console application with the Service Reference added. I have altered the app.config so the transferedMode="Streamed" and the maxReceivedMessageSize="2147483647" to again to allow large files to transferred.
Finally my program is :
namespace ConsoleApp { class Program { static void Main(string[] args) { string data = @"C:/Users/Admin/Desktop/IMG_0038.JPG"; StreamingServiceClient serviceHost = new StreamingServiceClient(); serviceHost.GetStream(data); } } }
When i run the application i get the error The remote server returned an unexpected response: (400) Bad Request.
Can anyone point me in the right direction so I can move on, so that once I have read in the file I can the save it.