如何获得默认的RESTful WCF服务以支持VARBINARY数据,以便可以传输较大的文档?

问题描述

| 我有一个运行良好的RESTful WCF服务。现在,我需要在后端SQL Server数据库中存储二进制信息(PDF文档)。因此,现在我的服务需要能够传输VARBINARY数据-我想是字节数组。当我进行设置并尝试使其正常工作时,出现以下错误:   超出了传入消息的最大消息大小配额(65536)。要增加配额,请在适当的绑定元素上使用MaxReceivedMessageSize属性。 唯一的问题是,我的Web.config中没有设置任何绑定元素。我在.SVC文件中使用默认的WebServiceHostFactory。看起来像这样:
<%@ ServiceHost Language=\"C#\" Debug=\"true\" Service=\"MyProject.MyService\" Factory=\"System.ServiceModel.Activation.WebServiceHostFactory\" %>
服务背后的代码如下所示:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract]
public class MyService
{
    public MyService()
    {
        XmlConfigurator.Configure();
    }

    [WebGet(UriTemplate = \"/docs/{docid}\")]
    [OperationContract]
    public Doc GetDoc(string docid)
    {
        [do some stuff to get a Doc object that has the byte array with the PDF file]
        return _doc;
    }
}
Doc是一个自定义类,如下所示:
public class Doc
{
    private string _title;
    private DateTime _docDate;
    private byte[] _pdfFile;

    public string Title
    {
        get{ return _title; }
        set{ _title = value; }
    }
    public DateTime DocDate
    {
        get{ return _docDate; }
        set{ _docDate = value; }
    }
    public byte[] PDFFile
    {
        get{ return _pdfFile; }
        set{ _pdfFile = value; }
    }
}
在客户端,我有以下代码可以访问服务。同样,Web.config中根本没有任何内容。我正在使用工厂。
public class MyClient
{
    WebChannelFactory<IMyService> cf;
    IMyService channel;

    public MyClient()
    {
        cf = new WebChannelFactory<IMyService>(new Uri(\"http://www.something.com/myservice.svc\"));
        channel = cf.CreateChannel();
    }

    public Doc GetDoc(string docid)
    {
        return channel.GetDoc(docid);
    }
}
我所阅读的所有内容都涉及在客户端和服务上都进行Web.config更改,以允许更大的消息大小。但是由于我在客户端和服务上都使用了Factory,所以我认为这行不通。代码中是否可以通过某种方式修改设置以允许更大的消息大小? 我查看了这个问题,并尝试执行它建议的操作,但没有成功。 WCF WebServiceHostFactory MaxReceivedMessageSize配置 任何帮助表示赞赏。     

解决方法

使用Stream作为ServiceContract中GetDoc的返回类型
Stream GetDoc(string docid)
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...