psabela
psabela

Reputation: 384

WCF from Silverlight without using Add Service Reference

David Betz describes in his article how to create reference to WCF without using "Add Service Reference" option: http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2

Once WCF service is created, these are the statements within the silverlight:

  BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
  EndpointAddress endpointAddress = new EndpointAddress("http://localhost:1003/Person.svc");
  IPersonService personService = new ChannelFactory<IPersonService>(basicHttpBinding, endpointAddress).CreateChannel();

...

How does one references the types (such as IPersonService interface) created in WCF from Silverlight when I do not use "Add Service Reference" to buid proxies?

Upvotes: 2

Views: 1370

Answers (3)

Sergey Orlov
Sergey Orlov

Reputation: 509

Only one thing that I would like detect. Often you need in namespaces with more complex support of NET within your WCF service. Therefore you must have real reasons to reference to Silverlight subset within your WCF service (or service library). There are many ways to use so named traditional ways by Add Service Reference. They are presented in good article enter link description here.

Upvotes: 0

Lex Lavnikov
Lex Lavnikov

Reputation: 1239

You also have to split all methods declaration in you IPersonService according to Async pattern (BeginXXX/EndXXX) since Silverlight supports only asyncronous WCF (even in background threads).

As a help to do this, you may add Service Reference, then copy generated IPersonService (all methods will be decoupled) from Reference.cs. Then you may remove the reference.

However, if your service contract is often changed, you have to repeat Add-Service procedure again, and starting from this, I would say, it's easier just use Add-Service-Reference feature, rather than sharing the contract with your app server.

Upvotes: 0

Valentin Kuzub
Valentin Kuzub

Reputation: 12073

Idea is to reference assemblies that contain WCF data contracts in silverlight application, and to do that you need to fool VS so it thinks assembly is a SL assembly, he describes this in detail here

http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight

and its not so easy, here is what needs to be done

Just use the same ILDasm/Edit/ILAsm procedure already mentioned to tell the assembly to use the appropriate Silverlight assemblies instead of the .NET assemblies. This is an extremely simple procedure consisting of nothing more than a replace, a procedure that could easily be automated with very minimal effort. It shouldn't take you much time at all to write a simple .NET application to do this for you. It would just be a simple .NET to Silverlight converter and validator (to test for assemblies not supported in Silverlight). Put that application in your Post Build Events (one of the top 5 greatest features of Visual Studio!) and you're done. No special binary hex value searching necessary. All you're doing is changing two well documented settings (the public key token and version).

Second solution is a file level solution , you use add link option on files that contain your required data contracts implementations to SL and make sure they only contain types that allow to build SL and dont reference a lot of external assemblies , usually those conditions should be met for WCF services & data contracts.

I can write more but it would be just the copy paste from that link

Upvotes: 2

Related Questions