gpt4 book ai didi

上传大文件的 WCF 问题,托管在 IIS 中

转载 作者:行者123 更新时间:2023-12-04 09:04:32 24 4
gpt4 key购买 nike

我正在尝试将大文件上传到 IIS 中托管的 WCF 服务。

我正在使用 Restful 和 Streaming 方法。

但我无法上传超过 64KB 的文件。

我通过更改 web.config 中所有与尺寸相关的元素进行了很多尝试。文件,但未能解决问题。

这是我的代码和配置,如果有人在代码中发现任何问题以及如何修复,请告诉我。

经营契约(Contract)

[OperationContract]
[WebInvoke(UriTemplate = "/UploadImage/{filename}")]
bool UploadImage(string filename, Stream image);

实现 经营契约(Contract)
public bool UploadImage(string filename, Stream image)
{
try
{
string strFileName = ConfigurationManager.AppSettings["UploadDrectory"].ToString() + filename;

FileStream fileStream = null;
using (fileStream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 1024;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = image.Read(buffer, 0, bufferLen)) > 0)
{
fileStream.Write(buffer, 0, count);
}
fileStream.Close();
image.Close();
}

return true;
}
catch (Exception ex)
{
return false;
}
}

web.config
  <system.serviceModel>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="http://localhost/WCFService1" behaviorConfiguration="web"
binding="webHttpBinding" bindingConfiguration="webBinding"
contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webBinding"
transferMode="Streamed"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00"
receiveTimeout="00:25:00" >
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>


<httpRuntime maxRequestLength="2097151"/>

服务托管在 IIS 中托管

客户端代码(控制台应用程序)
private static void UploadImage()
{
string filePath = @"F:\Sharath\TestImage\TextFiles\SampleText2.txt";
string filename = Path.GetFileName(filePath);

string url = "http://localhost/WCFService1/Service.svc/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "UploadImage/" + filename);

request.Accept = "text/xml";
request.Method = "POST";
request.ContentType = "txt/plain";

FileStream fst = File.Open(filePath, FileMode.Open);
long imgLn = fst.Length;
fst.Close();
request.ContentLength = imgLn;

using (Stream fileStream = File.OpenRead(filePath))
using (Stream requestStream = request.GetRequestStream())
{
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int byteCount = 0;
while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
{
requestStream.Write(buffer, 0, byteCount);
}
}

string result;

using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}

Console.WriteLine(result);
}

使用这么多代码,我可以上传 64KB 的文件,但是当我尝试上传大小超过 64KB 的文件时,出现如下错误:

The remote server returned an error: (400) Bad Request



我按照你说的做了,但仍然遇到同样的问题,这就是我的配置现在的样子,你能告诉我这里还缺少什么吗
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="http://localhost/WCFService1" behaviorConfiguration="web"
binding="webHttpBinding" bindingConfiguration="webBinding"
contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding transferMode="Streamed"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00"
name="webBinding">
<readerQuotas maxDepth="64"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
</binding>
</webHttpBinding>
</bindings>

最佳答案

wcf 的大数据传输问题:

我在 IIS 7、Windows 2008 服务器上托管了 wcf 4.0 服务。当我用小数据调用我的服务时说 4K 或 5K 字节然后请求很容易处理但是在尝试上传大尺寸时它给了我以下错误

  • 错误请求 400
  • 未找到文件 404 13,如 IIS 7 日志中所示
  • 没有端点监听服务“myservice url”

  • 在所有情况下,我都能够将小数据请求与我的客户端应用程序传输到服务器,但对于大消息。请求失败。

    我已经尝试了所有可用的方法来解决这个问题,但没有任何方法对我有用。

    在摸索了一周并阅读了所有文章和博客之后,我终于想到了以下内容
    <configuration>
    <system.serviceModel>
    <bindings>
    <basicHttpBinding>
    <binding name="myBinding" closeTimeout="00:10:00" maxBufferPoolSize="250000000" maxReceivedMessageSize="250000000" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" messageEncoding="Text">
    <readerQuotas maxDepth="4500000" maxStringContentLength="4500000" maxBytesPerRead="40960000" maxNameTableCharCount="250000000" maxArrayLength="4500000"/>
    </basicHttpBinding>
    </bindings>
    </system.serviceModel>
    </configuration>



    <system.web>

    <httpRuntime executionTimeout="4800" maxRequestLength="500000000"/>
    </system.web>

    <system.webServer>
    <security>
    <requestFiltering>
    <requestLimits maxAllowedContentLength="500000000"></requestLimits>
    </requestFiltering>
    </security>
    </system.webServer>

    <!-- other useful setting -->

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

    所以我认为这可能会对某人有所帮助....

    关于上传大文件的 WCF 问题,托管在 IIS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6317794/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com