Silverlight WCF Client Programmatic Configuration

We are developing a Silverlight Client to a server-side API exposed through WCF.

I am trying to move my WCF client code (which works great) from a configuration based model to a programmatic model. This will allow me to have a single "root" URL that I can apply at startup, and does not require installations to support extensive configuration files.

I am confusing converting my configurations to Silverlight compatible code.

If I have the following configuration for one of my services:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_ISilverlightHelper">
                    <binaryMessageEncoding />
                    <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
                        <extendedProtectionPolicy policyEnforcement="Never" />
                    </httpTransport>
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:50072/API/WCF/Silverlight/SilverlightHelper.svc"
                binding="customBinding" bindingConfiguration="CustomBinding_ISilverlightHelper"
                contract="API.WCF.Silverlight.ISilverlightHelper" name="CustomBinding_ISilverlightHelper" />
        </client>
    </system.serviceModel>
</configuration>

      

I cannot figure out how to generate the equivalent client-config code. At the moment I have:

CustomBinding customBinding = new CustomBinding();
// I see I need to do something with customBinding but the properties don't seem 
    // logical
    // I have used BasicHttpBinding, but it just returns with "Not Found" (the service does resolve to a valid URL)
BasicHttpBinding basicHttpBinding = new BasicHttpBinding() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50072/API/WCF/Silverlight/SilverlightHelper.svc");
ISilverlightHelper silverlightHelper= new ChannelFactory<ISilverlightHelper>(basicHttpBinding, endpointAddress).CreateChannel();
AsyncCallback asyncCallback = delegate(IAsyncResult result)
{
    ISilverlightHelper asyncSilverlightHelper = (ISilverlightHelper)result.AsyncState;
    string[] files=asyncSilverlightHelper.EndGetPlugInXapNames(result).ToArray();
};
silverlightHelper.BeginGetPlugInXapNames(asyncCallback, silverlightHelper);

      

Any hints would be appreciated. I've spent the entire morning Googling / Binging / Overflowing but didn't come across this scenario. Or maybe I am wrong ...

+2


source to share


1 answer


Sorted.

I created BinaryMessageEncodingBindingElement and HttpTransportBindingElements, added them to CustomBinding and it all works.



Here's my annotated code:

// create the binding elements
BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };

// add the binding elements into a Custom Binding
CustomBinding customBinding = new CustomBinding(binaryMessageEncoding,httpTransport);

// create the Endpoint URL (I'll use a configured URL later - all web services will then move as one)
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50072/API/WCF/Silverlight/SilverlightHelper.svc");

// create an interface for the WCF service
ISilverlightHelper silverlightHelper= new ChannelFactory<ISilverlightHelper>(customBinding, endpointAddress).CreateChannel();

// set-up the asynchronous callback
AsyncCallback asyncCallback = delegate(IAsyncResult result)
{
    ISilverlightHelper asyncSilverlightHelper = (ISilverlightHelper)result.AsyncState;
    string[] files=asyncSilverlightHelper.EndGetPlugInXapNames(result).ToArray();
};

// execute the call
silverlightHelper.BeginGetPlugInXapNames(asyncCallback, silverlightHelper);

      

+7


source







All Articles