当前位置:  开发笔记 > 编程语言 > 正文

RESTful WCF服务图像上传问题

如何解决《RESTfulWCF服务图像上传问题》经验,为你挑选了1个好方法。

您可以尝试覆盖任何内容类型,并使用IOperationBehavior将所有文件上载为application/octet-stream或text/plain.

public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter
{
    private IDispatchMessageFormatter innerFormatter;
    public string ContentTypeOverride { get; set; }

    public WebContentTypeAttribute(string contentTypeOverride)
    {
        this.ContentTypeOverride = contentTypeOverride;
    }


    // IOperationBehavior
    public void Validate(OperationDescription operationDescription)
    {

    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        innerFormatter = dispatchOperation.Formatter;
        dispatchOperation.Formatter = this;
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {

    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {

    }

    // IDispatchMessageFormatter
    public void DeserializeRequest(Message message, object[] parameters)
    {
        if (message == null)
            return;

        if (string.IsNullOrEmpty(ContentTypeOverride))
            return;

        var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];
        httpRequest.Headers["Content-Type"] = ContentTypeOverride;
    }

    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
        return innerFormatter.SerializeReply(messageVersion, parameters, result);
    }
}

而且你必须修改你的服务合同,看起来像这个

[OperationContract]
[WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")]
[WebContentType("application/octet-stream")]
bool UploadFile(string fileName, Stream fileContents);  

虽然,如果您从网页上传,数据不会以多部分/表格数据格式发布吗?



1> bendewey..:

您可以尝试覆盖任何内容类型,并使用IOperationBehavior将所有文件上载为application/octet-stream或text/plain.

public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter
{
    private IDispatchMessageFormatter innerFormatter;
    public string ContentTypeOverride { get; set; }

    public WebContentTypeAttribute(string contentTypeOverride)
    {
        this.ContentTypeOverride = contentTypeOverride;
    }


    // IOperationBehavior
    public void Validate(OperationDescription operationDescription)
    {

    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        innerFormatter = dispatchOperation.Formatter;
        dispatchOperation.Formatter = this;
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {

    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {

    }

    // IDispatchMessageFormatter
    public void DeserializeRequest(Message message, object[] parameters)
    {
        if (message == null)
            return;

        if (string.IsNullOrEmpty(ContentTypeOverride))
            return;

        var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];
        httpRequest.Headers["Content-Type"] = ContentTypeOverride;
    }

    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
        return innerFormatter.SerializeReply(messageVersion, parameters, result);
    }
}

而且你必须修改你的服务合同,看起来像这个

[OperationContract]
[WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")]
[WebContentType("application/octet-stream")]
bool UploadFile(string fileName, Stream fileContents);  

虽然,如果您从网页上传,数据不会以多部分/表格数据格式发布吗?


我真的很喜欢这个RESTful WCF,它似乎比标准的SOAP WS-*互操作性标准更清晰,更薄.msdn上有一篇很好的白皮书:http://msdn.microsoft.com/en-us/library/dd203052.aspx,对于想要了解更多信息的人来说.谢谢你的帮助Ben ... J
推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有