vinay singri
vinay singri

Reputation: 199

How to programatically add WCF data service(ODATA) reference in silverlight

I have a Oata i.e., WCF data service running.Is it possible for silverlight client to programatically add the reference of that service with only the URL of the service?

Currently I add the service reference by right clicking the project,Add service reference,Discover and then add the service reference.How can I do it programatically?

Upvotes: 0

Views: 681

Answers (2)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

It's not possible for several reasons, couple of them here:

  • Add Service Reference in Visual Studio generates code (the client side classes for the entities and other types exposed by the service). Silverlight doesn't have the necessary libraries to handle this task reasonably easy (lot of code would have to be rewritten/ported to silverlight for this).

  • In order for your code to actually use it, you would need to compile the generated code. There's no way to do this in SL, since it doesn't contain any compilers.

  • It's not very practicle to actual use, since your code using this would have to be able to deal with types it doesn't know up-front (the types generated by the code above). So you would have to use lot of reflection or other similar tricks.

If you want to write a client application which can access arbitrary OData service I would suggest you look at Microsoft.Data.OData.dll. It's part of the recent CTP: http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/announcing-wcf-data-services-oct-2011-ctp-for-net-4-and-silverlight-4.aspx And it implements readers and writers for OData format (something like XmlReader/XmlWriter, but for OData). In particular it doesn't require the knowledge of the service up-front, it can read any OData response.

Upvotes: 2

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

You should do the following:

BasicHttpBinding binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("<Yours Endpoint Address>");
var factory = new ChannelFactory<IYourServiceContract>(basicHttpBinding, endpointAddress);

Hope this will help.

Upvotes: 0

Related Questions