Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Advanced LightSwitch: passing initialization information to custom data sources (with Visual Basic)

When you build WCF RIA Services for Silverlight you can pass initialization information to the service, so that you can initialize fields or properties or any other members required by the same service.

When you have to create custom data source extensions for LightSwitch you still use WCF RIA Services but in a way that is a little bit difference according to some LightSwitch specific requirements. This also means that it is different the way you pass startup information to the service.

Actually you can use the connection string, which is not limited to that piece of information that you would normally use to connect to a database and that is what you are prompted to specify when you want to connect to a custom data source:

As you can see the Connection String box allows entering some information. This information:

  • is not necessarily a connection string for a database or another kind of service 
  • can be used to pass initialization information to the service as a string 
  • can be described with a custom description that we, developers, provide in code 

For a better understanding, imagine you have the following definition for a LightSwitch-enabled service where you want to initialize with a value supplied by the user the value of the field called personName:

    <Description("Enter startup information here")>
    Public Class PersonDataSource
        Inherits DomainService
        Private personName As String
        Private _connectionString As String
        Public Overrides Sub Initialize(context As System.ServiceModel.DomainServices.Server.DomainServiceContext)
            If WebConfigurationManager.ConnectionStrings(GetType(PersonDataSource).FullName) IsNot Nothing Then
                _connectionString = WebConfigurationManager.ConnectionStrings(GetType(PersonDataSource).FullName).ConnectionString
            End If
            If String.IsNullOrEmpty(_connectionString) = False Then
                Me.personName = _connectionString
                Me.personName = "Default-supplied value"
            End If
        End Sub
        Public Function GetPeople() As IQueryable(Of Person)
            'Your logic here....
        End Function
    End Class

Some considerations:

  1. The Description attribute allows specifying the descriptive text that will be shown in the connection string's box. This will help users to understand what they will need to enter and how
  2. The constructor is not used to receive startup information
  3. You need to override the Initialize method. This invokes the same-named method from its base class and via the ConnectionStrings property from the WebConfigurationManager object it retrieves the connection string of the service; this is right what the user entered inside the Connection Strings box
  4. The value of the connection string can be assigned (or parsed or analyzed) to fields, properties, or methods as arguments

For instance, if in the connection string box I entered my first name and last name, when the data source is initialized then the personName field would be automatically assigned with "Alessandro Del Sole" and therefore I could use this information according to my needs.

This is the appropriate way to pass initialization information to a custom data source in LightSwitch. Be sure to check out the official MSDN documentation about creating a custom data source for LightSwitch.


Print | posted on mercoledì 21 dicembre 2011 19:40 | Filed Under [ Visual Basic Visual Studio LightSwitch ]


No comments posted yet.

Post Comment

Please add 5 and 8 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET