Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Windows Client Development, Woodworking, .NET Programming, CNC, Nature, and other topics.

Subscribe

Subscribe to my feed
Add to Technorati Favorites

My Book

Order my upcoming book, Silverlight in Action, covering Silverlight 4, ViewModel/MVVM, WCF RIA Services, MEF and more

About Pete Brown

Pete Brown is a Microsoft Developer Division Community Program Manager, focusing on Windows Client Development as well as a former Microsoft Silverlight MVP and INETA Speaker. Pete writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. read more

Community Events


who's online

AddThis Social Bookmark Button

Creating a Silverlight WCF Binary Encoding Client in Code

In Silverlight, it can sometimes bet better to skip using the ServiceReferences.ClientConfig file and instead create your WCF clients from code, specifying the endpoint address. This was pretty simple in Silverlight 2 where all we had was basicHttpBinding. Silverlight 3, however, adds in the ability to use binary message encoding via the binaryMessageEncoding element in the server-side WCF config file – something that is on by default in the Silverlight Enabled WCF Service template.

Assuming you have a generated client (from a service reference) named “ExampleServiceClient”, here’s what the code looks like. The key entry is the BinaryMessageEncodingBindingElement. This may be obvious to folks who do tons of custom WCF work :)

private ExampleServiceClient BuildExampleServiceClient()
{
    BindingElementCollection elements = new BindingElementCollection();

    // order of entries in collection is significant: dumb.
    elements.Add(new BinaryMessageEncodingBindingElement());
    elements.Add(new HttpTransportBindingElement());

    CustomBinding binding = new CustomBinding(elements);

    // insert your favorite address resolution algorithm here
    EndpointAddress address = new EndpointAddress(
        new Uri("http://localhost:13519/Services/ExampleService.svc", UriKind.Absolute));

    ExampleServiceClient client = new ExampleServiceClient(binding, address);

    return client;
}

Thanks to Mike Taulty for pointing me in the right direction. I hope that saves you some trouble.

  Add to Technorati Favorites
Posted: Tuesday, July 14, 2009 7:07 PM by Pete.Brown
Filed under: , ,

Comments

Miguel MAdero said:

You'll want to change that Uri when you deploy, this code will help you dynamically get it:

public static string GetHost() 
{ 
  if (!HtmlPage.IsEnabled) 
    return "http://localhost:8732/"; 

  Uri uri = HtmlPage.Document.DocumentUri; 

  return String.Format("{0}://{1}:{2}/", uri.Scheme, uri.Host??"80", uri.Port); 
}
# July 15, 2009 10:02 AM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps
# July 15, 2009 3:21 PM

Pete.Brown said:

@Miguel

Thanks. That's a great piece of code to add to allow for dynamic service resolution based on the server that served up the SL app page.

Pete

# July 15, 2009 7:42 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout
# July 15, 2009 8:53 PM

#.think.in said:

#.think.in infoDose #37 (12th July - 19th July)
# July 18, 2009 9:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS