Sharing service interfaces and model in Silverlight, using WCF

流过昼夜 提交于 2019-12-23 09:49:50

问题


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:

  • One compiled targetting .NET framework 3.5, referenced by my web service project
  • Another compiled targetting the silverlight 2.0 framework, referenced by my silverlight app

Two versions of MyServicesContracts.dll which contains ICustomerService interface:

  • One compiled targetting .NET framework 3.5, referenced by my web service project
  • Another compiled targetting the silverlight 2.0 framework, referenced by my silverlight app

回答1:


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




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/274952/sharing-service-interfaces-and-model-in-silverlight-using-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!