Create Silverlight Enabled Service
WCF Services are easy to called in Silverlight, however there are few tweaks are required to change a Normal WCF Service to silverlight enabled WCF Service.
with
basicHttpBinding" contract="SilverlightEnabledWebService.IService1">
- Open Visual Studio 2008.
- Create New Project
- Select WCF
- WCF Service Library as shown in figure below
- Open App.Config and change below line
with
Now Add below in Service.cs
using System.ServiceModel.Activation;
Add following line just above the the class start
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
Complete Code for Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
namespace SilverlightEnabledWebService
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
namespace SilverlightEnabledWebService
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
Complete code for Service Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace SilverlightEnabledWebService
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
Comments