Romain Verdier
Romain Verdier

Reputation: 13011

Sharing service interfaces and model in Silverlight, using WCF

Say I have the following interface that I want to share between my server (a regular web service) and my client (a silverlight 2.0 application):

public interface ICustomerService
{
    Customer GetCustomer(string name);
}

My web service implements this interface, and references a class library where Customer type is defined.

Usually, if you want to consume this service from a WCF client, say a winforms app, you could share your model assembly and your service contract interfaces. Then, by using a ChannelFactory, you can dynamically create a proxy which implements your service interface. Something like:

ICustomerService myService = new ChannelFactory<ICustomerService>(myBinding, myEndpoint);
Customer customer = myService.GetCustomer("romain");

I basically want to do the same thing, but from a Silverlight 2.0 application. The silverlight ChannelFactory doesn't seems to act like the other one...

Do you know if it's possible ?

Note : Since a Silverlight application can only refers Silverlight projects, I have:

Two versions of MyModel.dll which contains Customer type:

Two versions of MyServicesContracts.dll which contains ICustomerService interface:

Upvotes: 5

Views: 3381

Answers (4)

Ahmed Ghoneim
Ahmed Ghoneim

Reputation: 7067

I know it's too late to provide a solution but it was my problem too and I found Portable Class Libraries. It's a perfect solution to your issue.

Upvotes: 0

Lampard
Lampard

Reputation:

Very short...


You can have your proxies created under the silverlight application adding a service reference to your service. When you do this, you'll have your proxies automaticaly generated on the client.


Your wcf services interfaces must be anotated with DataContract and OperationContract attributes and the POCO classes used with this services must have DataContract and DataMember attributes.


http://msdn.microsoft.com/en-us/library/cc197940(VS.95).aspx

Upvotes: 0

Steven Murawski
Steven Murawski

Reputation: 11255

I could be wrong, but I think if you decorate objects being returned by your WCF Service with the DataContract and DataMember attributes, you should be able to share objects between your Silverlight application and WCF service without creating the class in your client (should be handled by the proxy.

Upvotes: 0

Cristian Libardo
Cristian Libardo

Reputation: 9258

I think you will find this thread interesting. You can share code files between separate projects or compile a single project against multiple targets.

Upvotes: 3

Related Questions