views:

887

answers:

2

Hi,

We're developing a Silverlight Client onto a server-based API exposed via WCF.

I'm trying to move my WCF client code (which works fine) from a configuration-based model to a programmatic model. This will enable me to have a single "root" URL which I can apply at start-up and not require installations to have to maintain humongous configuration files.

I'm stuggling converting my configurations to Silverlight-capable code, though.

Where I have the configuration below 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 can't figure out how to create the equivelant 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 clues would be appreciated. I've spent all morning Googling/Binging/Overflowing but haven't come across this scenario. Or I might be just so far wrong ...

+4  A: 

Sorted it.

I created the BinaryMessageEncodingBindingElement and HttpTransportBindingElements, added them to the 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);
Program.X
dang! beat me by a few seconds.... :-)
marc_s
used this today for some unit tests. thanks.
Rick Glos
A: 

Thank you!

I figured out and had been using a very similar solution with Silverlight 3, but after I converted the project to Silverlight 4, it stopped working.

Your code sample helped me solve the issue.

Thank you very much!

Dmitriy Gorbachev