简体   繁体   English

使用WCF在Silverlight中共享服务接口和模型

[英]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): 假设我有以下界面,我想在我的服务器(常规Web服务)和我的客户端(silverlight 2.0应用程序)之间共享:

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

My web service implements this interface, and references a class library where Customer type is defined. 我的Web服务实现了此接口,并引用了定义Customer类型的类库。

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. 通常,如果您想从WCF客户端(例如winforms应用程序)使用此服务,则可以共享模型程序集和服务合同接口。 Then, by using a ChannelFactory , you can dynamically create a proxy which implements your service interface. 然后,通过使用ChannelFactory ,您可以动态创建实现服务接口的代理。 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. 我基本上想要做同样的事情,但是从Silverlight 2.0应用程序。 The silverlight ChannelFactory doesn't seems to act like the other one... Silverlight ChannelFactory似乎不像另一个......

Do you know if it's possible ? 你知道这是否可能?

Note : Since a Silverlight application can only refers Silverlight projects, I have: 注意:由于Silverlight应用程序只能引用Silverlight项目,因此我有:

Two versions of MyModel.dll which contains Customer type: 两个版本的MyModel.dll,其中包含Customer类型:

  • One compiled targetting .NET framework 3.5, referenced by my web service project 一个编译目标.NET框架3.5,由我的Web服务项目引用
  • Another compiled targetting the silverlight 2.0 framework, referenced by my silverlight app 另一个编译目标是silverlight 2.0框架,由我的silverlight app引用

Two versions of MyServicesContracts.dll which contains ICustomerService interface: 两个版本的MyServicesContracts.dll包含ICustomerService接口:

  • One compiled targetting .NET framework 3.5, referenced by my web service project 一个编译目标.NET框架3.5,由我的Web服务项目引用
  • Another compiled targetting the silverlight 2.0 framework, referenced by my silverlight app 另一个编译目标是silverlight 2.0框架,由我的silverlight app引用

I think you will find this thread interesting. 我想你会发现这个帖子很有趣。 You can share code files between separate projects or compile a single project against multiple targets. 您可以在单独的项目之间共享代码文件,也可以针对多个目标编译单个项目。

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. 这是您问题的完美解决方案。

我可能是错的,但我认为如果您使用DataContract和DataMember属性装饰WCF服务返回的对象,您应该能够在Silverlight应用程序和WCF服务之间共享对象而无需在客户端中创建类(应该处理通过代理。

Very short... 很短...


You can have your proxies created under the silverlight application adding a service reference to your service. 您可以在silverlight应用程序下创建代理,为服务添加服务引用。 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. 您的wcf服务接口必须使用DataContract和OperationContract属性进行分配,并且与此服务一起使用的POCO类必须具有DataContract和DataMember属性。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM