了解 WCF 客户端到服务器 [英] Understanding WCF Client to Server

查看:23
本文介绍了了解 WCF 客户端到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法完全掌握 Windows Communication Foundation 的特定功能.我读了一本又一本的教程,一本又一本.所以我对整个概念性质充满信心.

I'm having difficulty fully grasping a particular function of Windows Communication Foundation. I've read tutorial after tutorial, book after book. So the entire conceptual nature I feel confident on.

除了一部分;这是几乎像魔术一样的部分.这实际上使学习变得有些困难.

Except for one part; this is the part that is almost like magic. Which actually made the learning slightly difficult.

我将使用网络上最常见的示例.

I'll use the most common example on the web.

我将从 DataContract 开始:

[DataContract]
public class Customer
{

    // Declerations:
    private Guid id;
    private string firstName;
    private string lastName;
    private string emailAddress;

    [DataMembers]
    public Guid Id
    {
       get { return id; }
       set { id = value; }
    }

    [DataMember]
    public string FirstName
    {
         get { return firstName; }
         set { firstName = value; }
    }

    [DataMember]
    public string LastName
    {
         get { return lastName; }
         set { lastName = value; }
    }

    [DataMember]
    public string EmailAddress
    {
         get { return emailAddress; }
         set { emailAddress = value; }
    }

}

现在我已经创建了一个对象;我想向我的客户展示.

Now I've created an object; that I'd like to be exposed to my Client.

然后我创建了我的ServiceContract.

[ServiceContract]
public interface ICustomer
{

     [OperationContract]
     Customer AddCustomer(Customer info);

}

所以这就是我一直困惑自己的地方;假设您有一个客户端应用程序.你已经消费了服务.您在单独的程序集/命名空间中有三个文本框.客户输入标准:

So this where I keep confusing myself; lets say you have a Client-Side Application. You've consumed the service. You have three textboxes in a separate Assembly / Namespace. The Client puts in the criteria:

  • 名字
  • 姓氏
  • 电子邮件地址

如果您将这些文本框设置为日期;他们将在元数据中转移.但是在服务器上;我怎样才能把那个信息变量拉出来?我是否只引用了 私有 Guid 和私有字符串变量?

If you set those text boxes to the date; they will transfer over in Metadata. But on the server; how can I pull that information variable out? Do I just reference the private Guid and private string variables?

我看到了一个关于如何将它添加到数据库的教程;但我不完全理解 WCF 实际上在做什么.这与我想要的相似.我想获取客户端接口输入并将其写入数据库和单独的日志文件.

I saw a tutorial on how to add it to a database; but I don't fully comprehend what WCF is actually doing. Which is similar to what I'd like. I'd like to get the Client interface input and write it to a database and a separate log file.

我可以按照教程进行操作;但我想知道 Customer 对象数据及其变量是如何组装到服务器上使用的.

I could just follow the tutorial; but I want to know how the Customer object data and it's variables are being assembled for use on the server.

任何帮助都会很棒,一些澄清.

Any assistance would be amazing, some clarification.

对不起,如果我的问题很愚蠢;我不是要开始辩论.只是想了解如何提取这些变量并在服务器上使用它们.

Sorry if my question is stupid; I'm not trying to start a debate. Just want to understand how to pull those variables and use them on the server.

提前致谢.如果我没有正确格式化问题,请告诉我.我真的很想了解它在概念上的作用.

Thanks in advance. If I didn't format the question correctly please let me know. I'd really like to understand what it is conceptually doing.

更新:我的真正意图是了解 Client 接口如何引用该对象;所以当调用时,服务器有一个不为空的有效对象.

Update: My true intention is to understand how the Client interface references that object; so when the call is made the server has a valid object that isn't null.

文本框中的客户端类型 ---> 代理发送 ---> 反序列化 ---> 服务 ---> 序列化 ---> 使属性可供使用.

Client types in text box ---> Proxy Sends ---> De-serialized ---> Service ---> Serializes ---> Makes Property available for usage.

推荐答案

实际的类型,例如您的 Customer 类并没有真正通过网络传输.但是,这些类型中的公共信息是通过称为序列化的过程发送的.序列化允许以允许通过网络传输的方式表示类型.这通常使用 SOAP、JSON 或 XML 等格式表示.WCF 甚至允许您精确控制对象的序列化方式,允许您根据需要编写自己的格式化程序.基本上,当 AddCustomer 被调用时,WCF 会在服务器上构造一个 Customer 对象,对其进行序列化,然后通过网络发送这些位.

The actual types, such as your Customer class are not really transmitted across the wire. However, the public information within those types is sent across through a process called serialization. Serialization allows a type to be represented in a way that allows it to be transmitted over a network. This is often expressed using a format such as SOAP, JSON or XML. WCF even allows you to control exactly how objects are serialized, allowing you to write your own formatter if you want. Basically, when AddCustomer is called, WCF is constructing a Customer object on the server, serializing it, and sending those bits across the wire.

现在,在客户端上,您将有一个匹配的 Customer 对象,称为代理.它可能看起来像:

Now, on the client you would have a matching Customer object called a proxy. It might look something like:

public class Customer
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
}

基本上,一个缩小版本,只有服务器版本的数据成员,没有代码或逻辑.在客户端,Customer 的序列化表示被反序列化回本地代理类的一个实例,它可以用于各种客户端目的,包括绑定到本地 UI 元素.

Basically, a scaled down version with just the data members of the server version, with no code or logic. On the client, the serialized representation of Customer is deserialized back into an instance of this local proxy class, where it can be used for various client purposes, including being bound to local UI elements.

Web 服务可以使用 WSDL(一种 XML 格式,用于描述网络服务合同).Visual Studio(使用 wsdl.exe 工具)可以自动为你编写这些代理类,这让一切都变得神奇.

Web services can expose this type information using WSDL (which is an XML format for describing a web service contract). Visual Studio (using the wsdl.exe tool) can automatically write these proxy classes for you, which makes everything just work magically.

这篇关于了解 WCF 客户端到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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