简体   繁体   English

可以使用可移植类库通过通道工厂在Silverlight中使用WCF服务吗?

[英]Possible to consume a WCF service in Silverlight via a channel factory, using a Portable Class Library?

The Current Setup : Silverlight client consuming regular WCF services using RIA classes and generated service references. 当前设置 :Silverlight客户端使用RIA类和生成的服务引用来消耗常规WCF服务。

The Goal : Replace the service references and RIA classes with channel factories and a Portable Class Library (PCL) . 目标 :用通道工厂和可移植类库(PCL)替换服务引用和RIA类。 (XY goal: get rid of generated code.) (XY目标:摆脱生成的代码。)

The Approach So Far : 迄今为止的方法

The first part of this -- using channel factories -- is straightforward, and well documented. 第一部分 - 使用渠道工厂 - 很简单,并且有很好的文档记录。 Basically, the WCF service remains as-is, with the caveat that the ServiceContract has to be defined on an interface. 基本上,WCF服务保持原样,但需要注意必须在接口上定义ServiceContract Then on the client you create a ChannelFactory<IMyService> , supply the service URL, and the proxy is created like magic (no need for a service reference). 然后在客户端上创建一个ChannelFactory<IMyService> ,提供服务URL,并创建代理魔术(不需要服务引用)。

There is a wrinkle with Silverlight, namely that you have to use conditional compilation to define asynchronous operation contracts. Silverlight有一个问题,即你必须使用条件编译来定义异步操作契约。 (This will be important in a moment when I try to move the operation contract to the PCL.) Thus the service contract will look something like this: (当我尝试将操作合同移交给PCL时,这将非常重要。)因此,服务合同将如下所示:

[ServiceContract]
public interface IMyService
{
#if SILVERLIGHT
    [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginGetAString(AsyncCallback callback, object state);
    string EndGetAString(IAsyncResult result);
#else
    [OperationContract]
    string GetAString();
#endif
}

Note that it is (apparently) necessary to exclude the synchronous operation from the Silverlight compilation, as above. 请注意,(显然)必须从Silverlight编译中排除同步操作,如上所述。 Otherwise, the call to ChannelFactory.CreateChannel complains: 否则,对ChannelFactory.CreateChannel的调用会抱怨:

The contract 'IMyService' contains synchronous operations, which are not supported in Silverlight. 合同“IMyService”包含Silverlight不支持的同步操作。 Split the operations into "Begin" and "End" parts and set the AsyncPattern property on the OperationContractAttribute to 'true'. 将操作拆分为“Begin”和“End”部分,并将OperationContractAttribute上的AsyncPattern属性设置为“true”。 Note that you do not have to make the same change on the server. 请注意,您不必在服务器上进行相同的更改。

So far so good. 到现在为止还挺好。 The next step is to move code that is shared between client and server, from RIA .shared class files to a PCL. 下一步是将客户端和服务器之间共享的代码从RIA .shared类文件移动到PCL。 For the DataContract classes, this works perfectly well -- but trying to move the OperationContract class presents a problem. 对于DataContract类,这非常有效 - 但是尝试移动OperationContract类会出现问题。 The Silverlight ChannelFactory approach requires the conditional compilation of the synchronous method definition, as above, which (as is my understanding) is not possible in a PCL. Silverlight ChannelFactory方法需要对同步方法定义进行条件编译,如上所述,(在我的理解中)在PCL中是不可能的。

The Question : 问题

What is the most straightforward way (if any) to get this to work, short of abandoning the channel factory approach or maintaining an RIA project? 如果没有放弃渠道工厂方法或维护RIA项目,最简单的方法(如果有的话)是什么? For example, is there any way to tell Silverlight to simply ignore synchronous operations, rather than throwing an error? 例如,有没有办法告诉Silverlight简单地忽略同步操作,而不是抛出错误? Or any way to somehow exclude the synchronous definition for the Silverlight-targeted PCL (eg, a conditional build maybe)? 或以任何方式以某种方式排除Silverlight目标PCL的同步定义(例如,条件构建可能)?

Have you seen this post . 你看过这篇文章吗? This article will show you how to consume plain WCF services in Silverlight without the need to statically generate code for a service proxy (aka VS Add Service Reference wizard) 本文将向您展示如何在Silverlight中使用纯WCF服务,而无需静态生成服务代理的代码(又名VS添加服务引用向导)

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

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