The legendary WCF (2): What about service agreements?

Source: Internet
Author: User
The legendary WCF (1): Is this hard to learn?The legendary WCF (2): What about service agreements?Legendary WCF (3): multiple protocolsLegendary WCF (4): send and receive SOAP HeadersLegendary WCF (5): data protocols ()Legendary WCF (6): data protocols (B)Legendary WCF (7): "one-way" & "two-way"Legendary WCF (8): Play with message protocolsLegendary WCF (9): stream and file transferLegendary WCF (10): message interception and tamperingThe legendary WCF (11): Session)The legendary WCF (12): What is the use of server callback?Legendary WCF (13): group chat programThe legendary WCF (14): WCF can also be used as a chat program.

 

 

In the previous article, we threw out n questions: Is it hard to learn about WCF? Complicated? Can the complexity be simplified?

In fact, the answer to these questions is all dependent on your mentality. They all say "attitude determines everything". I don't know whether you believe it or not. I believe it anyway. First of all, those who dare to challenge themselves and are willing to accept new things will not feel hard to learn. "There are difficulties in the world, but they are afraid of people with their hearts." Furthermore, the process of WCF is complicated. net Framework is complex, but it is not difficult to learn because. net has a congenital advantage-integration and uniformity are seriously good, which is also my pleasure to study.. net. A unified country is always more powerful than a split country. Third, everything can be simplified. The key is what kind of mentality you hold. The highest realm of programming is to integrate the program with various academic disciplines such as natural philosophy.

 

Today, let's talk about the service agreement. If you just understand it literally, you will find it very abstract. After seven days and seven nights of negotiation, the two companies finally reached a consensus, so they signed the Cooperation Agreement. According to the cooperation agreement, the two companies share a certain degree of information on the ABCD project to facilitate cooperation. Think about this cooperation agreement, like the service agreement we mentioned here? Yes, the service agreement is actually nothing remarkable. The two companies that just signed the Cooperation Agreement are called "server" and "client" respectively ".

The service agreement tells the client which methods you can call and which data types you can use. The public and non-public interfaces allow you to call depend entirely on how the server defines the service agreement.

For example, the following agreement:

[Servicecontract]
Public interface iservicea
{
[Operationcontract]
Int addint (int A, int B );

[Operationcontract]
Double adddouble (double A, double B );

Float addfloat (float a, float B );
}

Implement this Agreement:

Public class myservice: iservicea
{
Public int addint (int A, int B)
{
Return A + B;
}

Public double adddouble (double A, double B)
{
Return A + B;
}

Public float addfloat (float a, float B)
{
Return A + B;
}
}

You can guess, in the code generated on the client, which methods can you see and which methods will not be seen? In the preceding service agreement, we define three methods, the first two of which contain operationcontractattribute, while the addfloat method is not labeled as a protocol. What will happen to the generated client code?

 

Is there a method missing? Yes, it means addfloat is missing. Remember? In the code above, the addfloat method is not labeled as an operation agreement.

Okay, now we add the operational agreement for addfloat, as shown below.

[Servicecontract]
Public interface iservicea
{
[Operationcontract]
Int addint (int A, int B );

[Operationcontract]
Double adddouble (double A, double B );

[Operationcontract]
Float addfloat (float a, float B );
}

 

At this time, you can see how many methods can be seen on the client.

This is all done, right.

 

Next let's take a look at the service namespace.

Servicecontractattribute has a namespace attribute used to set the service namespace. To avoid conflicts with other services on the Internet, the namespace must be unique (in fact, the XML namespace ), in most cases, we use the company Homepage Address or personal website address, because these addresses are unique.

When you do not set a namespace, let's take a look at what is the default namespace generated by the client code.

In the Solution Explorer window, click show all files, as shown in.

Open the. WSDL file.

 

<WSDL: types>
<XSD: schema targetnamespace = "http://tempuri.org/Imports">
<XSD: Import schemalocation = "http: // localhost: 9000/service? XSD = xsd0 "namespace =" http://tempuri.org/"/>
<XSD: Import schemalocation = "http: // localhost: 9000/service? XSD = xsd1 "namespace =" http://schemas.microsoft.com/2003/10/Serialization/ "/>
</XSD: schema>
</WSDL: types>

The default namespace is namespaces.

[Servicecontract (namespace = "http://MyApp.net")]
Public interface iservicea
{
[Operationcontract]
Int addint (int A, int B );

[Operationcontract]
Double adddouble (double A, double B );

[Operationcontract]
Float addfloat (float a, float B );
}

Now let's look at what the WSDL file will look like.

We found that the current WSDL file is different from the one just now. The custom namespace is shown above.

 

We may also ask, do I have to use the http: // METHOD FOR THE namespace? Can I use other strings? Don't ask. Just give it a try.

[Servicecontract (namespace = "fuckingdog")]
Public interface iservicea
{
.............

Let's take a look at how the WSDL describes the generated client code.

 

Now, the conclusion is displayed.

 

 

The servicecontractattribute class also has an attribute name. What happens when this attribute is set? Don't worry. Let's take a look at how it works without a name.

 

By default, the generated WSDL directly uses the name of the service agreement, that is, iservicea. So what if we modify the name attribute when defining a service agreement?

[Servicecontract (namespace = "fuckingdog", name = "HAHAHA")]
Public interface iservicea
{
.......

Compare the generated WSDL file.

 

Now, it turns into "HAHAHA". How can this be used? Haha, what do you do if you don't want the recipient to know your real name while chatting online? Hey, let's get a network name. Just get the name attribute for the service agreement, which is equivalent to a network name for it.

Also, this network name is not only available for service agreements, but also for operational agreements. Let's take a look.

[Servicecontract (namespace = "fuckingdog", name = "HAHAHA")]
Public interface iservicea
{
[Operationcontract (name = "operatfirst")]
Int addint (int A, int B );

[Operationcontract (name = "operatsecond")]
Double adddouble (double A, double B );

[Operationcontract (name = "operatthird")]
Float addfloat (float a, float B );
}

Guess what the client code will look like after this change?

 

Right? In this way, the caller does not know who your last name is.

 

How is it? Is WCF interesting? The complete code list of the Server Sample in this article is as follows.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel;
Using system. servicemodel. description;

Namespace Server
{
[Servicecontract (namespace = "fuckingdog", name = "HAHAHA")]
Public interface iservicea
{
[Operationcontract (name = "operatfirst")]
Int addint (int A, int B );

[Operationcontract (name = "operatsecond")]
Double adddouble (double A, double B );

[Operationcontract (name = "operatthird")]
Float addfloat (float a, float B );
}

Public class myservice: iservicea
{
Public int addint (int A, int B)
{
Return A + B;
}

Public double adddouble (double A, double B)
{
Return A + B;
}

Public float addfloat (float a, float B)
{
Return A + B;
}
}

Class Program
{
Static void main (string [] ARGs)
{
Using (servicehost host = new servicehost (typeof (myservice), new uri ("http: // localhost: 9000/service ")))
{
Wshttpbinding binding = new wshttpbinding ();
Binding. Security. mode = securitymode. None;
Host. addserviceendpoint (typeof (iservicea), binding, "contract1 ");
Host. description. behaviors. Add (New servicemetadatabehavior () {httpgetenabled = true });
Host. Opened + = (SD, ARG) => console. writeline ("the service has been started. ");
Try
{
Host. open ();
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
Console. readkey ();
Host. Close ();
}
}
}
}

Finally, let's talk about the WSDL. This guy is enough for us to understand. Why? We don't have to write the WSDL document by ourselves, or even say that it doesn't affect programming if you don't understand it, the powerful Visual Studio will generate the required code when we reference the service, just like using a general class.

If you want to know about WSDL, look at this. It's good and concise. Http://www.w3school.com.cn/wsdl/index.asp

 

 

Turn to it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.