Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C# 4.0

WCF Services for Silverlight (No Configuration Required)

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
13 Sep 2010CPOL1 min read 24.9K   272   15   2
WCF Services for Silverlight without configuration in config file

Introduction

This article shows how to develop configuration-less WCF Services for Silverlight client.

Background

It is pretty complex to maintain the configuration part of WCF services, if more services are adding / modifying, very frequently.

It will be a better idea to develop a service library for our Silverlight client, without any configurations in the config file. The following class diagram says about such a library, which is the scope of this article.

Fig-1.jpg

Using the Code

The vital class in the above class diagram is SLServiceHost, which inherits from ServiceHost and overrides ApplyConfiguration method. There, you can implement our custom bindings and Meta data behaviour, MEX, etc.

Below is the code snippet:

C#
protected override void ApplyConfiguration()
{            
    base.ApplyConfiguration();
    CustomBindings();
    EnableMetaDataBehaviorAndAddMexEndPoint();
}

There is a custom service host factory, SLServiceHostFactory, which inherits from ServiceHostFactoryBase and creates the instance of our SLServiceHost.

In our custom service(MyService.svc), in the XAML, we set the factory and service code behind attributes as follows:

XML
<%@ ServiceHost Language="C#"
Factory="Silverlight.Services.SLServiceHostFactory,Silverlight.Services"
Service="Silverlight.Services.MyService" %>

Run Silverlight.Services.Web application from VS2010. The port is set to 33333.

Now run your wcftestclient utility from VS command prompt and add the service and test.

Click to enlarge image

Points of Interest

  1. Service Library with no configuration in Web.Config
  2. Single Contract Interface, ISLService

History

  • 13th September, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Automation Anywhere
India India
Working as Lead Architect at Automation Anywhere.

Comments and Discussions

 
GeneralVirtualServices Pin
Glen Banta22-Sep-10 14:37
Glen Banta22-Sep-10 14:37 
GeneralMy vote of 4 Pin
Nils B14-Sep-10 22:12
Nils B14-Sep-10 22:12 
Good work Shine. Hope to see more articles from you!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.