【发布时间】:2011-09-29 09:03:14
【问题描述】:
我在 ASP.NET 应用程序中托管了 WCF 服务,并尝试使用流模式从另一个客户端 ASP.NET 应用程序上传文件。 我不断收到以下错误:
远程服务器返回一个 意外响应:(400)坏 请求。
我已经浏览了网络寻求帮助,但无济于事。
主机配置:
<bindings>
<basicHttpBinding>
<binding name="FileTransferServicesBinding"
transferMode="Streamed"
messageEncoding="Mtom"
sendTimeout="01:05:00"
maxReceivedMessageSize="10067108864">
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="FileTransferServiceBehavior" name="WebServiceHost.TransferService">
<clear />
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="FileTransferServicesBinding" contract="WebServiceHost.ITransferService">
<identity>
<dns value="localhost"/>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:11291/TransferService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="FileTransferServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
合同代码:
[ServiceContract]
public interface ITransferService
{
[OperationContract]
RemoteFileInfo DownloadFile(DownloadRequest request);
[OperationContract]
void UploadFile(RemoteFileInfo request);
[OperationContract]
string TestMethod();
}
[MessageContract]
public class DownloadRequest
{
[MessageBodyMember]
public string FileName;
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
客户端配置:
<bindings>
<basicHttpBinding>
<binding name="FileTransferServicesBinding" sendTimeout="01:05:00"
maxReceivedMessageSize="10067108864" messageEncoding="Mtom"
transferMode="Streamed" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:11291/TransferService.svc/"
binding="basicHttpBinding" bindingConfiguration="FileTransferServicesBinding"
contract="TransferServiceReference.ITransferService" name="FileTransferService"
kind="">
<identity>
<dns value="localhost" />
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
客户端代码:
ITransferService clientUpload = new TransferServiceClient();
string datetime = clientUpload.TestMethod();
var uploadRequestInfo = new RemoteFileInfo();
uploadRequestInfo.FileName = FileUpload1.FileName;
uploadRequestInfo.Length = FileUpload1.PostedFile.ContentLength;
uploadRequestInfo.FileByteStream = FileUpload1.PostedFile.InputStream;
clientUpload.UploadFile(uploadRequestInfo);
clientUpload.TestMethod() 和 clientUpload.UploadFile(uploadRequestInfo) 的调用均失败。在缓冲模式下调用 clientUpload.TestMethod(),所以问题在于流模式下的配置。
如果能就此事找到任何帮助,我将不胜感激。谢谢。
【问题讨论】:
-
我看不出配置有任何明显错误。您可能需要在服务上Enable Tracing 才能更深入地挖掘
标签: c# asp.net wcf streaming wcf-binding