The legendary WCF (1): Is this hard to learn?

Source: Internet
Author: User
Tags baseuri
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.

 

 

Is WCF hard to learn?

Yes, many people may ask this question. This is also a profound and serious question, including Allah.

Some people also say: "It's easy to learn how a technology can be simplified ." Perhaps, humans cannot get rid of laziness when they are born. However, some people think that, in many cases, do people always like to complicate simple things, many people think that the complicated WCF calculation is not a "Myth?

I will not answer this question. Now we have to do one thing. Let's take my step and write a program together.

This solution contains two projects: server and client.

1. When vs 2010 is started, we recommend that you use version 2010 or later (version 2012 RC is also supported). Because the higher the version, the better it is, the better express is. Don't pay for it. I use it every day.

2. Create two console projects. I don't need to teach you any more. After the project is created, your vs project is similar to the one shown in.

3. on Solution Explorer, find the solution node, right-click it, and select "properties" from the pop-up menu ".

4. In the displayed window, select "selected content" in "startup project", as shown in.

In this way, it is easier to start debugging. You can leave it unspecified. Haha.

5. Select the "server" project. Generally, we need to complete the server first.

Right-click the server project reference and select Add reference from the shortcut menu... ", in the subsequent window, confirm the selection.. Net tab. servicemodel, and click OK.

6. Open the program. CS file of the server project. First, introduce several namespaces that may be used.

Using system. servicemodel;
Using system. servicemodel. description;

7. Define a service agreement, which contains a testmethod method. A service agreement is an interface.

[Servicecontract]
Public interface iservice
{
[Operationcontract]
String testmethod ();
}

We can see that the service agreement is an interface, but it is attached with the servicecontractattribute feature, and the methods in the interface are also attached with the operationcontractattribute feature. As a service operation, it can be called by client programs, if the operationcontractattribute feature is not added, this method is not disclosed to the client.

8. Define a class and implement the service agreement defined above.

Public class myservice: iservice
{
Public String testmethod ()
{
Return "Xin chunge, undergraduate course. ";
}
}

9. Define server-related parameters in the main entry point and start the service.

Static void main (string [] ARGs)
{
// Base address Uri, required, HTTP Scheme
Uri baseuri = new uri ("http: // localhost: 8008/service ");

Using (servicehost host = new servicehost (typeof (myservice), baseuri ))
{
// Add an endpoint to the server
Wshttpbinding binding = new wshttpbinding ();
// Security verification is not required here
Binding. Security. mode = securitymode. None;
Host. addserviceendpoint (typeof (iservice), binding, "my ");
// To allow vs to generate client code, that is, the WSDL document, add the following actions:
Servicemetadatabehavior mdbehavior = new servicemetadatabehavior ()
{
Httpgetenabled = true
};
Host. description. behaviors. Add (mdbehavior );

// If the service starts successfully, a prompt is displayed to process the opened event.
Host. Opened + = (sender, e) => console. writeline ("the service has been started. ");
// Start the server
Try
{
Host. open ();
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}

// Add this sentence to prevent the program from being executed.
Console. readkey ();
// Close the server
Host. Close ();
}
}

At this time, first confirm that the server project is currently selected, and then run. If the project is successful, you will see the content shown in.

 

 

Now, the server is complete. The next step is the client.

This is simple. First find the \ bin \ DEBUG directory where the server is located and run server.exe to ensure that the service starts successfully.

Select the client project, right-click on "Reference", and select "add service reference" from the shortcut menu"

 

In the pop-up window, enter the defined base address, namely http: // localhost: 8008/service. Remember, you must use the base address, which is used to create the servicehost instance, do not use the endpoint address.

Click the "go" button. After the service is read correctly, enter the name of the namespace you want and click OK.

 

At this time, we can write code on the client,

Static void main (string [] ARGs)
{
WS. serviceclient client = new ws. serviceclient ();
Console. writeline (client. testmethod ());

Console. readkey ();
}

Run the command to complete the first WCF application.

 

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.