我有一个从服务器下载内容的 WCF 客户端。
服务契约(Contract)是;
[OperationContract]
[WebGet(
UriTemplate = "/my/service/url/{method}/{filename}?tradeId={tradeId}&docType={docType}&language={language}&version={version}",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
Stream GetDocument(string method, string filename, string tradeId, string docType, string version, string language);
返回类型是 Stream。我所做的只是将该流写入一个文件,它就可以工作了。
现在,我想对此进行修改。我想知道下载文档的 MIME 类型。我知道它在服务器上设置正确。我只需要取回它。
我对 WCF 经验不多,也不知道该怎么做。有人可以告诉我吗?
非常感谢
最佳答案
您必须有权访问 OperationContext
或 WebOperationContext
。要在客户端上实现这一点,请使用 OperationContextScope
:
using (var scope = new OperationContextScope((IContextChannel)proxy))
{
Stream document = proxy.GetDocument(...);
string contentType = WebOperationContext.Current.IncomingResponse.ContentType;
}
关于c# - 获取 WCF 响应的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5350758/