获取内容WCF响应的类型

Kev*_*vin 3 c# wcf mime http

我有一个从服务器下载内容的WCF客户端.

服务合同是;

[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);
Run Code Online (Sandbox Code Playgroud)

返回类型是Stream.我所做的只是将该流写入文件并且它可以工作.

现在,我想对此进行修改.我想知道下载文档的MIME类型.我知道它在服务器上设置正确.我只需要检索它.

我对WCF没什么经验,也不知道怎么做.有人能告诉我吗?

非常感谢

Lad*_*nka 5

您必须访问OperationContextWebOperationContext.要在客户端上实现这一点,请使用OperationContextScope:

using (var scope = new OperationContextScope((IContextChannel)proxy))
{
    Stream document = proxy.GetDocument(...);
    string contentType = WebOperationContext.Current.IncomingResponse.ContentType;
}
Run Code Online (Sandbox Code Playgroud)