Understanding WCF Client to Server

2019-09-05 08:58发布

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.

I'll start with the 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.

So I then create my 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:

  • First Name
  • Last Name
  • Email Address

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?

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.

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.

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.

标签: c# wcf service
2条回答
【Aperson】
2楼-- · 2019-09-05 09:00

I am not sure but may be this is what you are looking for Data Transfer and Serialization

In particular you can check DataContractSerializer

You can check this article too : Serialization in Windows Communication Foundation

查看更多
ら.Afraid
3楼-- · 2019-09-05 09:16

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.

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; }
}

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 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.

查看更多
登录 后发表回答