gpt4 book ai didi

c# - WCF:将流式处理与消息协定结合使用

转载 作者:IT王子 更新时间:2023-10-29 04:39:35 26 4
gpt4 key购买 nike

我正在尝试将 WCF 流式处理与消息协定结合使用,因为除了流本身之外我还需要其他参数。

基本上我正在创建一个文件上传和下载服务,上面有一些额外的逻辑。

不幸的是,当我尝试从浏览器访问服务以检查一切是否正常时,出现以下错误:

“/”应用程序中的服务器错误。契约(Contract)“IFileTransferService”中的操作“UploadFile”使用具有 SOAP header 的 MessageContract。 None MessageVersion 不支持 SOAP header 。

不幸的是,谷歌搜索并没有产生任何对我有帮助的重要结果。你们能帮帮我吗?这里是服务的详细信息(由于篇幅原因,我删除了下载部分)。

[ServiceContract(Namespace = "http://www.acme.org/2009/04")]
public interface IFileTransferService
{
[OperationContract(Action = "UploadFile")]
void UploadFile(FileUploadMessage request);
}

[MessageContract]
public class FileUploadMessage
{
[MessageHeader(MustUnderstand = true)]
public FileMetaData Metadata { get; set; }

[MessageBodyMember(Order = 1)]
public Stream FileByteStream { get; set; }
}

[DataContract(Namespace = "http://schemas.acme.org/2009/04")]
public class FileMetaData
{
[DataMember(Name="FileType", Order=0, IsRequired=true)]
public FileTypeEnum fileType;

[DataMember(Name="localFilename", Order=1, IsRequired=false)]
public string localFileName;

[DataMember(Name = "remoteFilename", Order = 2, IsRequired = false)]
public string remoteFileName;
}

我尝试同时使用 basichttpbinding 和 customhttp 绑定(bind),但效果不佳:

<customBinding>
<binding name="customHttpBindingStream">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport transferMode="Streamed" maxReceivedMessageSize="2147483647"/>
</binding>
</customBinding>

更新:在线阅读文档似乎确实可以使用 MessageContracts 进行流式传输。请参阅 MSDN ( Large Data and Streaming) 的示例:

Programming Model for Streamed Transfers

The programming model for streaming is straightforward. For receiving streamed data, specify an operation contract that has a single Stream typed input parameter. For returning streamed data, return a Stream reference. [...] This rule similarly applies to message contracts. As shown in the following message contract, you can have only a single body member in your message contract that is a stream. If you want to communicate additional information with the stream, this information must be a carried in message headers. The message body is exclusively reserved for the stream content.

[MessageContract]
public class UploadStreamMessage
{
[MessageHeader]
public string appRef;
[MessageBodyMember]
public Stream data;
}

我还看到人们完成文件上传和下载服务的博客文章与我试图放在一起的内容非常相似(例如 here)。

更新 2我已经尝试创建一个小型控制台并使用 basicHttpBinding 自行托管该服务,它在那里工作得很好。我开始相信问题可能出在 IIS 上。有什么想法吗?

更新 3请参阅我自己的答案。

最佳答案

我终于找到了错误所在:它与 Soap 版本、流等无关...我只是拼错了我自己的服务名称 (!),改用 FileTransfer FileTransferService

最后 basicHttpBinding 非常好,我不需要求助于自定义绑定(bind)。

原始(坏)版本:

<service 
behaviorConfiguration="serviceBehavior"
name="Acme.Service.FileTransfer">
<endpoint address=""
name="basicHttpStream"
binding="basicHttpBinding"
bindingConfiguration="httpLargeMessageStream"
contract="Acme.Service.IFileTransferService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

新(固定)版本:

<service 
behaviorConfiguration="serviceBehavior"
name="Acme.Service.FileTransferService">
<endpoint address=""
name="basicHttpStream"
binding="basicHttpBinding"
bindingConfiguration="httpLargeMessageStream"
contract="Acme.Service.IFileTransferService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

我仍然不能说错误消息对理解这里发生的事情有任何帮助....

如果您对整个服务感兴趣,可以通过以下链接在我的博客上找到更多详细信息:File Transfer with WCF

关于c# - WCF:将流式处理与消息协定结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1339857/

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