我是 WCF biginner。我做了一个简单的 WCF 服务和一个客户端来上传文件。但是当上传超过 100 KB 时,我收到了 400 个错误请求。我在互联网上搜索并找到了一些关于修改 max* Length 或 max* *size 的解决方案。但我还在挣扎。
所以,我想请教专家如何解决这个问题。
服务代码在这里。
[ServiceContract]
public interface IService1
{
[OperationContract]
void SaveFile(UploadFile uploadFile);
}
[DataContract]
public class UploadFile
{
[DataMember]
public string FileName { get; set; }
[DataMember]
public byte[] File { get; set; }
}
public class Service1 : IService1
{
public void SaveFile(UploadFile uploadFile)
{
string str = uploadFile.FileName;
byte[] data = uploadFile.File;
}
}
服务配置在这里。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="64000000"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings>
<webHttpBinding>
<binding maxBufferSize="64000000" maxReceivedMessageSize="64000000" maxBufferPoolSize="64000000">
<readerQuotas maxDepth="64000000" maxStringContentLength="64000000" maxArrayLength="64000000" maxBytesPerRead="64000000" />
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
客户端代码在这里。
private void button1_Click(object sender, RoutedEventArgs e)
{
FileInfo info = new FileInfo(@"C:\Users\shingotada\Desktop\4.png");
byte[] buf = new byte[32768];
Stream stream = info.OpenRead();
byte[] result;
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buf, 0, buf.Length);
if (read > 0)
{
ms.Write(buf, 0, read);
}
else
{
break;
}
}
result = ms.ToArray();
}
UploadFile file = new UploadFile();
file.File = result;
file.FileName = "test";
ServiceReference2.Service1Client proxy2 = new ServiceReference2.Service1Client();
proxy2.SaveFile(file); //400 bad request
}
客户端配置在这里。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="64000000"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53635/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference2.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
谢谢。