Я создаю свой первый сервис WCF, который использует Stream.Конечный продукт, который я хочу, это выбрать файл на моем жестком диске и сохранить его в базе данных.Поэтому первым шагом, который я пытаюсь выполнить, является чтение выбранного файла .jpg.
Мой контракт выглядит следующим образом:
namespace Web
{
interface name "IStreamingService"
[ServiceContract]
public interface IStreamingService
{
[OperationContract]
Stream GetStream(string data);
}
}
Мой IService:
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;
}
}
}
Мой файл Web.Config выглядит следующим образом: httpRuntime maxRequestLength = "2147483647" добавлен, чтобы обеспечить потоковую передачу большого файла.
<!-- 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>
Затем у меня есть консольное приложение со службойСсылка добавлена.Я изменил app.config, чтобы transferedMode = "Streamed" и maxReceivedMessageSize = "2147483647" снова позволили передавать большие файлы.
Наконец, моя программа:
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);
}
}
}
Когда я запускаю приложение, я получаю сообщение об ошибке Удаленный сервер возвратил неожиданный ответ: (400) Плохой запрос.
Может ли кто-нибудь указать мне правильное направление, чтобы я мог двигаться дальше, поэтомучто, прочитав файл, я смогу его сохранить.