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

时间:2011-04-14 16:43:36

标签: wcf rest

我有一个运行良好的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更改以允许更大的邮件大小。但由于我在客户端和服务上使用工厂,我认为这不会起作用。在代码中是否有某种方法可以修改我的设置以允许更大的消息大小?

我看了这个问题并尝试按照它的建议行事,但它没有用。 WCF WebServiceHostFactory MaxReceivedMessageSize configuration

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在ServiceContract中使用Stream作为GetDoc的返回类型

Stream GetDoc(string docid)
相关问题