如何在不添加服务参考的情况下进行呼叫WCF服务的更简单说明 [英] Simpler Explanation of How to Make Call WCF Service without Adding Service Ref

查看:180
本文介绍了如何在不添加服务参考的情况下进行呼叫WCF服务的更简单说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解Silverlight 2中的WCF服务,作者David Betz解释了如何调用Web服务而不在客户端应用程序中添加服务引用.我在WCF上有两周的经验,因此这篇文章使我很头疼.特别是,尽管作者给出了很多代码片段,但没有说明去哪里.在文章中,他为web.config文件提供了两个不同的代码段,但没有阐明正在发生什么.

In Understanding WCF Services in Silverlight 2, the author, David Betz, explains how to call a web service without adding a service reference in the client application. I have a couple of weeks experience with WCF, so the article was over my head. In particular, although the author gave a lot of code snippets, but does not say what goes where. In the article, he provides two different code snippets for the web.config file, but does not clarify what's going on.

查看源代码,有四个项目和两个web.config文件.

Looking at the source code there are four projects and two web.config files.

到目前为止,我一直在使用标准的Silverlight项目配置,其中一个项目用于Web服务,另一个项目用于Silverlight客户端.

So far, I have been using the standard Silverlight project configuration of one project for the web service and one for the Silverlight client.

首先,本文中描述的过程是否适用于标准的两个项目配置?我想会的.

Firstly, does the procedure described in the article work with the standard two project configuration? I would think it would.

其次,有人知道一个简单的例子吗?我对此非常感兴趣,但是想查看在创建新的Silverlight项目时生成的默认两个项目设置中的源代码,或者查找有关如何执行此操作的分步说明(例如,添加一个类)名为xxx.cs并添加此代码...,打开web.config并添加以下行...)

Secondly, does anyone know of a simpler example? I am very interested in this, but would like to either see source code in the default two project setup which is generated when a new Silverlight project is made, or find a step by step description of how to do this (eg, add a class called xxx.cs and add this code..., open web.config and add these lines...)

非常感谢 迈克·托马斯

Many thanks Mike Thomas

推荐答案

首先,请讲一点哲学...

First, a little philosophy...

如果您是未编写的WCF服务的使用者,则向客户端添加服务引用实际上是您必须启用与该WCF服务进行交互的唯一机制.否则,您将无法知道服务合同的样子,更不用说其数据和消息合同了.

If you are a consumer of a WCF service that you did not write, adding a service reference to your client is really the only mechanism you have to enable interaction with that WCF service. Otherwise, you have no way of knowing what the service contract looks like, much less its data and message contracts.

但是,如果您同时控制客户端和WCF服务本身,则向客户端添加服务引用是很方便的,但是最近我坚信不要使用它.首先,在您更改合同以记住要更新服务参考的前几次之后,这变得很麻烦.就我而言,我有几个使用WCF服务的不同C#项目,因此我必须记住要更新每个项目.其次,创建服务引用会复制WCF服务中已经定义的合同定义.了解这一点的含义很重要.

However, if you are in control of both the client and the WCF service itself, adding a service reference to the client is a nice convenience, but I've recently been convinced not to use it. For one, it becomes a nuisance after the first few times you change your contract to remember to update your service reference. And in my case, I have several different C# projects that are consuming the WCF service, so I have to remember to update each one of them. Second, creating a service reference duplicates the contract definitions that are already defined in your WCF service. It is important to understand the implications of this.

假设您的WCF定义了以下类型.

Let's say your WCF defines the following type.

[DataContract]
public class Person
{
    [DataMember] public string FirstName {get; set;}
    [DataMember] public string LastName {get; set;}
}

当您向客户端添加服务引用时,将通过元数据交换(MEX)端点检索与该类关联的元数据,并在客户端上创建该类的精确副本,以便您的客户端对其进行编译" .因此,您的WCF服务具有Person类的定义,您的客户端也具有定义,但是它们是两个不同的类定义.

When you add a service reference to your client, the metadata associated with this class is retrieved through the metadata exchange (MEX) endpoint, and an exact replica of this class is created on the client side that your client "compiles" against. So your WCF service has a definition of the Person class, and so does your client, but they are two different, distinct class definitions.

鉴于此,将Person类抽象为一个单独的程序集,然后在WCF服务和客户端之间共享将更有意义.额外的好处是,当您在此共享程序集中更改合同定义时,您不再需要更新客户端中的服务引用,因为它已经在引用共享程序集.那有道理吗?

Given this, it would make more sense to abstract the Person class into a separate assembly that is then shared between the WCF service and the client. The added benefit is that when you change the contract definitions within this shared assembly, you no longer have to update the service reference within the client because it is already referencing the shared assembly. Does that make sense?

现在是您的问题.就个人而言,我只在C#项目中使用了WCF,而没有在Silverlight中使用.但是,我认为事情并没有根本不同.鉴于此,我建议您在dnrTV上观看 Extreme WCF 视频.它提供了有关如何绕过服务参考功能的分步指南.

Now to your question. Personally, I've only used WCF within C# projects, not Silverlight. However, I do not think things are radically different. Given that, I would suggest that you watch the Extreme WCF video at dnrTV. It gives a step-by-step guide for how to bypass the service reference feature.

希望这会有所帮助.

这篇关于如何在不添加服务参考的情况下进行呼叫WCF服务的更简单说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆