0

I have a RESTful WCF service that is working fine. Now I have a need to store binary information (PDF documents) in the back end SQL Server database. So now my service needs to be able to transport VARBINARY data - I suppose as a byte array. When I set this up and try to get it working I receive the following error:

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

The only problem is that I don't have any binding elements set up in my Web.config. I am using the default WebServiceHostFactory in my .SVC file. It looks like this:

<%@ ServiceHost Language="C#" Debug="true" Service="MyProject.MyService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

The code behind for my service looks like this:

[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 is a custom class that looks like this:

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; }
    }
}

On the client side of things I have the following code that accesses the service. Again there is nothing in the Web.config at all. I am using the Factory.

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);
    }
}

Everything that I have read talks about making Web.config changes on both the client and the service to allow for a larger message size. But since I am using the Factory on both the client and the service I don't think that will work. Is there some way in code that I can modify my setup to allow for a larger message size?

I looked at this question and tried to do what it recommends but it didn't work. WCF WebServiceHostFactory MaxReceivedMessageSize configuration

Any help is appreciated.

Community
  • 1
  • 1
Corey Burnett
  • 7,312
  • 10
  • 56
  • 93

1 Answers1

0

Use Stream as your return type for GetDoc in the ServiceContract

Stream GetDoc(string docid)
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • GetDoc returns a custom class. Doc is a class I created that has the different attributes of a PDF document - title, date created, etc. One of the attributes is the actual PDF document itself. Currently that property is a byte array. Should I make that a Stream instead? – Corey Burnett Apr 14 '11 at 18:39
  • I edited the post to include an example of the Doc class that is returned. – Corey Burnett Apr 14 '11 at 18:44
  • @Corey Sorry I thought your Doc was your entire document. I always recommend that people split out their metadata and their actual file as two different resources. To my knowledge the DataContractSerializer will not do any kind of binhex encoding of your doc. You may have been luck implementing IXmlSerializable and doing it manually. – Darrel Miller Apr 14 '11 at 20:12