使用Wcf服务读取然后保存文件
我正在创建我的第一个使用 Stream 的 WCF 服务。我想要的最终产品是从硬盘中选择一个文件并将其保存到数据库中。因此,我尝试完成的第一步是读取选定的 .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)错误的请求。
任何人都可以为我指出正确的方向,以便我可以继续前进,所以一旦我读入文件,我就可以保存它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有两个选择。第一种是使用一种二进制编码消息传输机制。如果您不担心与其他平台的互操作性(IOW、.NET 到 .NET 通信),您可以选择二进制编码器,或者如果您想与不<的其他系统通信,则可以使用 MTOM /em> .NET。在这种情况下,您可以将签名更改为字节数组,并确保传输实际字节,而不是流。根据图像的大小以及一次传送的图像数量,这可能会或不会对系统内存造成压力。
第二种选择是使用流式传输。在这种情况下,您只能使用基本 HTTP 绑定(不能使用 WS HTTP 绑定,它不支持流式传输)。您可以使用
Stream
返回参数保留消息签名,但设置绑定以支持流式传输。Niraj Bhatt 有一篇很棒的博客文章,标题为“MTOM 与流式传输与压缩 – WCF 上的大型附件”,其中详细介绍了如何实现上述每个选项,以及有关何时可能更详细的信息比其他有用。
You have two options. The first is to use one of the binary-encoded message transmission mechanisms. You can choose the binary encoder if you don't have concerns about interoperability with other platforms (IOW, .NET-to-.NET communications) or you can use MTOM if you want to talk to other systems that are not .NET. In this case you would change the signature to a byte array and make sure to transmit the actual bytes, not a stream. Depending on the size of the images and how many you are delivering at a time, this could or could not place a strain on memory in your system.
The second option is to use streaming. In this case, you can only use the Basic HTTP binding (not the WS HTTP binding, that does not support streaming). You would retain the message signature with the
Stream
return parameter, but set up the binding to support streaming.Niraj Bhatt has a great blog entry titled "MTOM vs. Streaming vs. Compression – Large attachments over WCF" which goes into detail about how to implement each of the options presented above, as well as more information as to when one might be more useful over the other.