简体   繁体   English

如何在 WCF 控制台应用程序中增加文件大小限制

[英]How to increase the file size limit in WCF console application

I've having a very simple WCF service (a console application for file upload).我有一个非常简单的 WCF 服务(用于文件上传的控制台应用程序)。 I keep getting error (400) bad request.我不断收到错误(400)错误请求。 It works when I upload small files (4kb) but failing for 700kb.当我上传小文件(4kb)但失败 700kb 时,它可以工作。

From the readings I've done from stack overflow and other, I'll have to increase the MaxReceivedMessageSize.根据我从堆栈溢出和其他方面所做的读数,我将不得不增加 MaxReceivedMessageSize。 This was implemented using a custom class and overriding the OnOpening method but it still didn't work.这是使用自定义 class 并覆盖 OnOpening 方法实现的,但它仍然不起作用。

I'm testing with a console application using the webclient我正在使用 webclient 使用控制台应用程序进行测试
outputBytes = webClient.UploadData(baseUrl + "/uploads/2." + filenameOnly, File.ReadAllBytes(filename));

Also, I'm using the WebServiceHost as in另外,我正在使用 WebServiceHost
var uri = new Uri("http://localhost:8000");
var svc = new WebServiceHost(typeof (UploadService), uri);

How do I solve this issue?我该如何解决这个问题? PS: Application does not have a config file so I'll looking at how to set this in code. PS:应用程序没有配置文件,所以我将看看如何在代码中设置它。 If not and a config file is needed, then what should be the content.如果没有并且需要配置文件,那么内容应该是什么。

Notes: I found this link Bad Request Error 400 - WCF Client where it was explained that those properties are only valid for soap based services.注意:我发现此链接Bad Request Error 400 - WCF Client解释说这些属性仅对基于 soap 的服务有效。 He suggested updating the web.config.他建议更新 web.config。 Since this is a console application, I'm wondering how this can be done'由于这是一个控制台应用程序,我想知道如何做到这一点'

regards.问候。

Did you have a look at the documentation ?你看过文档吗?

It covers both code and configuration.它涵盖了代码和配置。

You can set the maximumreceivedmessage programatically like this:您可以像这样以编程方式设置最大接收消息:

 var binding = new wsHttpBinding(); // or whatever binding you are using
 binding.MaxReceivedMessageSize = Int32.MaxValue; 
 var wcfClient = new WCFServiceTestClient(binding, strServiceURL); 

Hopefully this will be enough to solve your problem, but you might also want to consider chopping up the file into bits (batching) before sending it to the server.希望这足以解决您的问题,但您可能还需要考虑在将文件发送到服务器之前将其切成小块(批处理)。 Sending very large chunks of data can incurr a huge memory penalty as the client has to serialize the entire thing up front before sending it to the server (and then the same to deserialize it on the server side).发送非常大的数据块可能会导致巨大的 memory 损失,因为客户端必须先序列化整个数据,然后再将其发送到服务器(然后在服务器端反序列化它)。

Edit: Based on your additional comment (below), the code on the application hosting the service might look something like this:编辑:根据您的附加评论(如下),托管服务的应用程序上的代码可能如下所示:

        WebServiceHost webServiceHost = new WebServiceHost(typeof(UploadService), uri);
        WebHttpBinding binding = new WebHttpBinding();
        binding.MaxReceivedMessageSize = Int32.MaxValue;
        webServiceHost.AddServiceEndpoint(typeof(IUploadService), binding, "WebServiceHost");
        webServiceHost.Open();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM