Learn from scratch WCF 5 (generate client)

Source: Internet
Author: User

There is a picture of the truth. If you are also a cainiao, let's take a look at the implementation of this cainiao step by step.

Follow the course to learn about WCF 4 (bearer service) from scratch. We have released this service in IIS in this section.

Let's take a look at how to generate a client.

1. Obtain service agreements, bindings, and addresses

-- Generally, you can use servicemodel metadata utility tool(svcutil.exe) to complete this operation.

Download the metadata from the service and convert it to the hostedSource codeFile

Create a client application that can be used to configure the WCF client objectProgramConfiguration File

This agreementCodeCompile as a client application or another assembly. Then, the client application can use this Assembly to create a WCF client object.

You can use the configuration file to configure the client object to connect to the service correctly.

2. Create a WCF client object.

-- The WCF client is a local object of a WCF Service. The client can use this representation to communicate with the remote service.

-- The WCF client type can implement the target service agreement. Therefore, after creating a service agreement and configuring it, You can directly use this client object to call service operations.

-- The method call is converted to a message during the WCF runtime, and then the messages are sent to the service, listening for replies, and using these values as return values or out parameters (or ref parameters) return to the WCF client object

3. Call the operation

-- After the client object is created and configured, create a try/Catch Block. if the object is a local object, call the operation in a painful way, and then close the WCF client object.

-- When the client application calls the first operation, WCF automatically opens the basic channel and closes the basic channel when the object is recycled.

-- Do not use using blocks to call the WCF method.

Call method example

 
Serviceclient SC = new serviceclient (); try {console. writeline (SC. getdata (1, 111); SC. close ();} catch (timeoutexception timeout) {SC. abort ();} catch (communicationexception commexception) {SC. abort ();}

4. handling errors

-- Any system. servicemodel. faultexception object caused by the soap error returned by the Operation

-- At least set the application to be able to handle possible system. timeoutexception and system. servicemodel. communicationexception exceptions.

5. Configure and protect the client

--The security requirements of service agreement have been declared in the service agreement interface, and if svcutil.exe has created a configuration file, the file usually contains a binding that can meet the security requirements of the Service. However, in some cases, more security configurations may be required, such as configuring client creden.

Now let's create a client to call the WCF Service published in IIS.

Step 1: Open the command tool as shown in the following figure:

Open the tool and enter the following command:

/Out: output the Service proxy code,/config: generate the client service configuration.

/Out: C: \ clientservice. CS/config: C: \ app. config http: // 192.168.0.5/wcfiisservice/service. SVC enter the following command to generate the corresponding file in the corresponding location

Step 2 Add the console application in solution in the previous section, add the two files that have just been generated to the console application, and add system. servicemodel

Step 3: add the calling code in the main function of the console application.

ClassProgram {Static VoidMain (String[] ARGs) {serviceclient SC=NewServiceclient ();StringSTR = SC. getdata (111); Console. Write (STR); console. Readline ();}}

Step 4: Run F5 to check the effect. Of course, we only make simple calls to main. It is best to follow the above steps.3. Call the operationCall

Here we only talk about one way to call WCF on the client. In fact, there are other methods for calling.

Another method I know for the moment is to right-click the WCF client and find and call the service. Then, enter the service address to reference the service and call it in the client program ..

Download the sample code in this section. Click to download the connection.

 

 

 

 

 

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.