Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WCF在Silverlight中共享服务接口和模型_C#_.net_Wcf_Silverlight - Fatal编程技术网

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

C# 使用WCF在Silverlight中共享服务接口和模型,c#,.net,wcf,silverlight,C#,.net,Wcf,Silverlight,假设我有以下接口,我想在服务器(常规web服务)和客户端(silverlight 2.0应用程序)之间共享该接口: 我的web服务实现了这个接口,并引用了定义了Customertype的类库 通常,如果您想从WCF客户端(例如winforms应用程序)使用此服务,您可以共享您的模型程序集和服务契约接口。然后,通过使用ChannelFactory,您可以动态创建实现服务接口的代理。比如: ICustomerService myService = new ChannelFactory<ICus

假设我有以下接口,我想在服务器(常规web服务)和客户端(silverlight 2.0应用程序)之间共享该接口:

我的web服务实现了这个接口,并引用了定义了
Customer
type的类库

通常,如果您想从WCF客户端(例如winforms应用程序)使用此服务,您可以共享您的模型程序集和服务契约接口。然后,通过使用
ChannelFactory
,您可以动态创建实现服务接口的代理。比如:

ICustomerService myService = new ChannelFactory<ICustomerService>(myBinding, myEndpoint);
Customer customer = myService.GetCustomer("romain");
iccustomerservice myService=newchannelfactory(myBinding,myEndpoint);
Customer=myService.GetCustomer(“romain”);
我基本上想做同样的事情,但是来自Silverlight 2.0应用程序。silverlight
ChannelFactory
的行为似乎与另一个不同

你知道这是否可能吗?

注意:由于Silverlight应用程序只能引用Silverlight项目,因此我有:

MyModel.dll的两个版本包含
客户
类型:

  • 一个针对.NETFramework 3.5的编译版本,由我的web服务项目引用
  • 另一个目标是silverlight 2.0框架,由我的silverlight应用程序引用
MyServiceContracts.dll的两个版本,其中包含
ICCustomerService
接口:

  • 一个针对.NETFramework 3.5的编译版本,由我的web服务项目引用
  • 另一个目标是silverlight 2.0框架,由我的silverlight应用程序引用

    • 我想你会发现这很有趣。您可以在不同的项目之间共享代码文件,也可以针对多个目标编译单个项目。

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

      非常简短


      您可以在silverlight应用程序下创建代理,为您的服务添加服务引用。当您这样做时,您将在客户端上自动生成代理


      您的wcf服务接口必须具有DataContract和OperationContract属性,并且与此服务一起使用的POCO类必须具有DataContract和DataMember属性



      我知道现在提供解决方案已经太迟了,但这也是我的问题,我发现了。这是解决您问题的完美方案

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