Saturday, 29 May 2010

Silverlight project initial configuration

There are several steps to make initial configuration of any silverlight project. We can use RIA service for communication but I will describe here with WCF service. If we use VS 2008 we have to install silverlight toolkit and VS 2008 service pack 3. But for VS 2010 silverlight framework is already exist.

1.       First create an Silverlight type project. There are two types of project will automatically generate. One is web project and another is silverlight project.

2.       Silverlight works in client side so it produce a .xap file which exist in web project ClientBin folder. Being a client side application it needs service to communicate with server.  XAP file reated settings generate when we take silverlight type project. In web project there is ProjectNameTestPage.aspx file. It contains necessary settings.

3.       Now add Silverlight enabled WCF service (.svc file extension) to web project. Service file open automatically. There are few key words or class like [ServiceContract(Namespace = "")] above class name. [OperationContract] is above method name.  

4.       When we will create method in service we have to use the key word [OperationContract] above every method to introduce method publically.

5.       In web.config file there is tag <system.serviceModel/> under which few configuration code will generate. It is basically setup for service communication. In <bindings/> tag there is tag name <customBinding0/> remove the tag and its inside code and paste the following code.


            <binding name="PCServicesBinding" closeTimeout="00:10:00" openTimeout="00:10:00"

              receiveTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647"

              maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"


              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"

                maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />



You can change the name and can configure the value of tags.

6.       Now in <endpoint/> tag under <service/> tag change the name of binding = “basicHttpBinding” and bindingConfiguration = “PCServicesBinding”. It is the name of tag.

7.       Run service (.svc) file by right click and “View in browser”. Now go to the Silverlight project add “Add Service Reference” click on discover. All the available service in web project will shown as reference in silverlight project. Try to expand service reference. Click on Advance option, uncheck “Reuse types in referenced assemblies”, click ok.

Now project is ready for coding with WCF service. We can create more configurations in this project. Basically when we deploye a web project service url also need to change along with project url. Service get this url from “ServiceReferences.ClientConfig” file. We can make a configurable file for services from where we can easily change the url of services.