Search code examples
c#web-serviceswcfrestmaxreceivedmessagesize

Configuring a WCF REST service as a client?


Is it possible to have a WCF service also perform as a web service client?

If this IS possible, can you offer me some guidance on how to configure the client settings in the config file?

The main issue I'm having is that I'm sending large messages to my main REST service. When that message is relayed to the secondary service it seems that the response triggers a "MaxReceivedMessage" too large error. I've tried to configure the CLIENT setting for my REST service and have not been successful.

Which config do I define it in...the app.config or the web.config?

It seems that I am doing it wrong as no matter where I declare the CLIENT settings the binding is ignored.

Here is my APPLICATION config for the REST service.

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IBBIImageWarpService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/BBIImageWarp" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IBBIImageWarpService"
                contract="ServiceReference1.IBBIImageWarpService" name="BasicHttpBinding_IBBIImageWarpService" />
        </client>
    </system.serviceModel>
</configuration>

HERE IS THE ENDPOINT METHOD ON MY REST SERVICE that is failing:

        public ServiceResponse<DataContracts.BBIImgObject> WarpImage(DataContracts.BBIImgObject imgObject)
        {
            try
            {

                writeMessage("converting to JSON");

                string JSON = new JavaScriptSerializer().Serialize(imgObject);

                BasicHttpBinding binding = new BasicHttpBinding();

//SHOULD I ADD THE MAXRECEIVEDMessageSize TO THIS BINDING???

                EndpointAddress address = new EndpointAddress("http://localhost:8080/BBIImageWarp");

                ServiceReference1.BBIImageWarpServiceClient ImgWarpSvc = new ServiceReference1.BBIImageWarpServiceClient(binding, address);

                string rslt = ImgWarpSvc.WarpImageJSON(JSON);

                DataContracts.BBIImgObject cloneImgObject = new DataContracts.BBIImgObject();
                cloneImgObject.Base64EncodedImageData = rslt;
                cloneImgObject.BodyTypeID = imgObject.BodyTypeID;

                return new ServiceResponse<DataContracts.BBIImgObject>(String.Empty, ServiceResponse<DataContracts.BBIImgObject>.ResponseTypeEnum.BbiSuccess, cloneImgObject);
            }
            catch (Exception ex)
            {
                writeMessage(ex.Message);
                return new ServiceResponse<DataContracts.BBIImgObject>(ex.Message, ServiceResponse<DataContracts.BBIImgObject>.ResponseTypeEnum.BbiFailure, null);
            }
        }

Solution

  • You can easily make a client using the same binaries

    1. Include the DLL of your service class to your client project.
    2. Create a channel factory from your service interface.
    3. Consume the channel factory.

    For more information according to your requirements, please see http://msdn.microsoft.com/en-us/library/ms576132(v=vs.110).aspx

    For the max received error, you need to do the following: The max message size quota for incoming messages (65536) ....To increase the quota, use the MaxReceivedMessageSize property

    Or from the code:

    WebHttpBinding binding = new WebHttpBinding();
    binding.MaxReceivedMessageSize = 2147483647;
    

    Similarly on the client side also.