To create a WCF service application
Create a Silverlight application
Create additional
To add a WCF service in a WCF service application
Referencing a class library
Add a WCF Service
The. cs file
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.Text;usingSilverlightwcf.dal;namespacesilverlightwcfservices{//Note: Using the rename command on the Refactor menu, you can change the interface name "Iworkerinfoservice" in code and configuration files at the same time. [ServiceContract] Public InterfaceIworkerinfoservice {[OperationContract] [Serviceknowntype (typeof(Workerinfo))] List<WorkerInfo>Getworkerinfo (); }}
The. svc file
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.Text;usingsil. DAL;usingsil. BLL;usingSystem.ServiceModel.Activation;namespacesil. services{//Note: Using the rename command on the Refactor menu, you can change the class name "Workerservice" in Code, SVC, and configuration files at the same time. //Note: In order to start the WCF test client to test this service, select Workerservice.svc or WorkerService.svc.cs in Solution Explorer and start debugging. //[ServiceBehavior (Includeexceptiondetailinfaults = true)]//[aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)] Public classWorkerservice:iworkerservice { PublicList<workerinfo>Getallworker () {return NewBll_worker (). Getallworker (); } }}
Add a cross-domain file
File contents:
<?XML version= "1.0" encoding= "Utf-8"?><Access-policy> <cross-domain-access> <Policy> <Allow-fromhttp-request-headers="*"> <DomainURI="*"/> </Allow-from> <grant-to> <ResourcePath="/"include-subpaths= "true"/> </grant-to> </Policy> </cross-domain-access></Access-policy>
Adding a WCF service reference in a Silverlight application
Import namespaces
using Silverlightwcfclient.workerinfoservicereference;
code example:
Public voidGetworkerinfo () {workerinfoserviceclient client=Newworkerinfoserviceclient (); Client.getworkerinfocompleted+ = client_getworkerinfocompleted;//vs version, this line of code will varyClient.getworkerinfoasync (); } voidClient_getworkerinfocompleted (Objectsender, Getworkerinfocompletedeventargs e) { if(E.error! =NULL) return; if(E.result.count >0) {Dgworkerinfo.itemssource=e.result.tolist (); } }
Create SILVERLIGHT+WCF