0

I want to create and consume a WCF Service in Silverlight. I have created a service that returns this model from a database:

namespace SilverlightWithWCFService.Web
{
    [DataContract]
    public class Customer
    {
        [DataMember]
        public string CustomerName { get; set; }
        [DataMember]
        public string CompanyName { get; set; }
        [DataMember]
        public string ContactName { get; set; }
    }
}

The service looks like this:

namespace SilverlightWithWCFService.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode =  AspNetCompatibilityRequirementsMode.Allowed)]
    public class SampleService
    {

        [OperationContract]
        public List<Customer> CustomerList()
        {
            var custList = new List<Customer>();

            // populate custList

            return custList;
            }
        }
    }
}

In my Silverlight application, I added a Service Reference. This method calls the service operation:

 public Page()
 {
    InitializeComponent();
    SampleServiceClient client = new SampleServiceClient();
    client.CustomerListCompleted += new EventHandler<CustomerListCompletedEventArgs>(client_CustomerListCompleted);
    client.CustomerListAsync();
 } 

 void client_CustomerListCompleted(object sender, CustomerListCompletedEventArgs e)
 {
     CustomerGrid.ItemsSource = e.Result;
 }

So my question is: I don't know how the Silverlight work with WCF. Do I have to serialize something on WCF side and deserialize the return value on client side? If so, what code is missing? (Where?)

UPDATE:

I think based on some online questions. Should I deserialize the returned e.Result in the completed event code?

Community
  • 1
  • 1
Bigeyes
  • 1,508
  • 2
  • 23
  • 42

1 Answers1

1

Do I have to serialize something on WCF side and deserialize the return value on client side?

No, when you consume the webservice the underlying code will do all that for you.

Don't get hung up on it being Silverlight. Just think of Silverlight as the same as a console application. Whatever one has to do in the console app to consume the webservices, one will have to do in Silverlight. The only difference is that you will need to handle the calls in an async manner, but that is separate from the consuming of the webservice which your question pertains.


Note there was a competing technology to do all the updates of the webservice during a compile. That was called RIA services and that is a different animal all together.

I would recommend you use WCF web services, but update the size of the send/receive buffers for you will max those out easily on any true data transfers.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • But it is a web application. The data is transferred over the wire. – Bigeyes Feb 03 '17 at 15:27
  • 1
    @Bigeyes if you have a problem with the code you're showing, then explain your problem. You do not need to manually deserialize using WCF, that's the power of WCF. It doesn't matter at all whether you use WCF in a Console Application, Web Application or in Silverligt. – CodeCaster Feb 03 '17 at 15:28
  • I updated the class. Say I have ` [DataContract] & [DataMember]` in my class. Which means I have to use DataContractSerializer? – Bigeyes Feb 03 '17 at 15:34
  • @Bigeyes can you please stop firing unrelated questions in comments to someone who answered your question, and edit your question to ask the question you actually want answered instead? – CodeCaster Feb 03 '17 at 15:36
  • @Bigeyes Distill your operation down to a test console client, if the attributes you mention need specialized processing, it will need to be done in Silverlight. The attributes you mentioned are standard operating procedures in WCF. Turn on `Hidden Files` in the solution explorer and examine the hidden file created when one consumes a webservice. See how it is handling the operations. – ΩmegaMan Feb 03 '17 at 15:54
  • Yes. I guess that it works in that way. I have a hard time to find out how it works under cover. There are a lot hidden files. I don't know which one it is. Is it a cs file in `Service References` folder? – Bigeyes Feb 03 '17 at 16:01
  • @Bigeyes that one is it. :-) – ΩmegaMan Feb 03 '17 at 17:29