views:

856

answers:

1

I'm wondering why the class Binding in WCF doesn't have a property ReaderQuotas, while its subclasses BasicHttpBinding and WSHttpBinding does.

This fact makes coding a little hard. For me, I use below code to extract binding information from MEX endpoint URI. However, it just got Binding. If I want to change ReaderQuotas of the binding, I have to downcast it to subclasses of Binding, but I cannot tell the exact binding at runtime.

    public static void LoadMex(string serviceMexUri,
        ContractDescription contract,
        out EndpointAddress endpointAddress,
        out Binding serviceBinding)
    {
        EndpointAddress mexAddress = new EndpointAddress(serviceMexUri);
        MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);
        mexClient.ResolveMetadataReferences = true;
        mexClient.OperationTimeout = TimeSpan.FromSeconds(30);

        MetadataSet metaSet = mexClient.GetMetadata();
        WsdlImporter importer = new WsdlImporter(metaSet);
        ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

        foreach (ServiceEndpoint ep in endpoints)
        {
            // we just use one endpoint for now.
            if ((ep.Contract.Namespace == contract.Namespace) &&
                 (ep.Contract.Name == contract.Name))
            {
                endpointAddress = new EndpointAddress(ep.Address.Uri);
                serviceBinding = ep.Binding;
                return;
            }
        }
        throw new ApplicationException(String.Format("no proper endpoint is found from MEX {0}", serviceMexUri));
    }

Anybody know why WCF is designed in such way?

Is there any way to work around this limitation?

A: 

The reason is that bindings are intended to function as generic communication infrastructure and ReaderQuotas is a SOAP specific object. This is why you only see it on the bindings that are intended to be used with SOAP message transfers.

An "as" statement to try the cast to the types you want to support is probably your best option here.

jezell
How are reader quotas SOAP specific? At best, they are XML-specific, and even so, I'm not that sure about it. Don't they apply to the JSon formatters as well, given that they are implemented internally as xml readers/writers?
tomasr
I checked MSDN, it seems a lot of Binding subclasses has ReaderQuotas, not just SOAP binding.
Morgan Cheng