上传文件 WCF Web API 端点

发布于 2024-11-10 08:22:56 字数 97 浏览 3 评论 0原文

有没有办法将文件上传到 WCF Web API 端点?如果是这样,我如何访问 Network Stream 来读取数据?

非常感谢任何帮助。

谢谢。

Is there any way to upload a file to WCF web API endpoint? If so, how can I access Network Stream to read data?

Any help is very much appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傻比既视感 2024-11-17 08:22:56

假设您正在谈论客户端:

一种方法是创建一个派生自 HttpContent 的 FileContent 类。在覆盖中,当 HTTPClient 准备好接收流时,您将获得 NetworkStream。

如果您询问服务器端,那么您只需获取 HTTPRequestMessage 并访问 httpRequestMessage.Content.ContentReadStream 属性即可。


更新

默认情况下,WCF 传输仅限于以 65K 发送消息。如果你想发送更大的数据,你需要启用“流传输模式”,并且需要增加 MaxReceivedMessageSize 的大小,它的作用是防止有人通过上传大量文件来破坏你的服务器。

因此,您可以使用绑定配置来完成此操作,也可以使用代码来完成此操作。这是在代码中执行此操作的一种方法:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]);  //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB

Assuming you are talking about on the client side:

One way to do it is to create a FileContent class that derives from HttpContent. In the overrides you will be given the NetworkStream when the HTTPClient is ready to receive the stream.

If you are asking about the server side, then you can simply get hold of the HTTPRequestMessage and access the httpRequestMessage.Content.ContentReadStream property.


Update

By default the WCF Transport is limited to sending messages at 65K. If you want to send larger you need to enable "Streaming Transfer Mode" and you need to increase the size of MaxReceivedMessageSize, which is there just as a guard to prevent someone killing your server by uploading a massive file.

So, you can do this using binding configuration or you can do it in code. Here is one way to do it in code:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]);  //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB
云裳 2024-11-17 08:22:56

此答案仅旨在通过 Web API 预览版 6 添加一些详细信息。

TransferModeMaxRecievedMessageSize 是通过 WebApiConfigurationClass 新公开的。

var builder = new WebApiConfiguration();
builder.TransferMode = TransferMode.Streamed;
builder.MaxReceivedMessageSize = 1024 * 1024 * 10;

var serviceRoute = new WebApiRoute(route, new WebAPIServiceHostFactory() { Configuration = builder }, typeof(MyService));

我喜欢它!

This answer is only designed to add some details with Web API preview 6.

TransferMode and MaxRecievedMessageSize are new exposed through the WebApiConfigurationClass.

var builder = new WebApiConfiguration();
builder.TransferMode = TransferMode.Streamed;
builder.MaxReceivedMessageSize = 1024 * 1024 * 10;

var serviceRoute = new WebApiRoute(route, new WebAPIServiceHostFactory() { Configuration = builder }, typeof(MyService));

I like it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文