gpt4 book ai didi

c# - ASP.NET 的 WCF 流问题

转载 作者:太空狗 更新时间:2023-10-30 01:25:36 25 4
gpt4 key购买 nike

我有一个托管在 ASP.NET 应用程序中的 WCF 服务,我正在尝试使用流模式从另一个客户端 ASP.NET 应用程序上传文件。我不断收到以下错误:

The remote server returned an unexpected response: (400) Bad Request.

我浏览过网络寻求帮助,但无济于事。

主机配置:

    <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(),因此问题出在流模式下的配置上。

如果能就此事寻求任何帮助,我将不胜感激。谢谢。

最佳答案

我遇到了同样的问题,我发现这篇文章解释了如何解决您的错误。 http://www.c-sharpcorner.com/uploadfile/BruceZhang/stream-operation-in-wcf/ .

希望对您有所帮助!

关于c# - ASP.NET 的 WCF 流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6592664/

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