Learning about WCF (3)-developing a WCF client program

Source: Internet
Author: User
Tags configuration settings

Previous learn WCF (2) -- The Development of the WCF Service introduces the development of service programs. In this article, there are three main solutions for developing a client program:

Add a web reference

Use svcutil.exe

Programming scheme

1. Add a web reference

This is basically the same as the method for adding a reference web service. In the Add reference dialog box, enter URL: http: // localhost: 39113/wcfservicetext/wcfstudenttext. SVC

Start a name for WCF and click Add reference to complete the following tasks:

(1) download the WSDL file from the specified URL for the Student Management Service

(2) generate the proxy class wcfstudenttext, which is the proxy of the server wcfstudenttext and implements the server contract istuservicecontract.

(3) configure the response generation

Now we can use the proxy wcfstudenttext to communicate with the student information management service. Add a page to the site and put a gridview and objectdatasource

 

Code
<Div>
<Asp: gridview id = "gridview1" runat = "server" backcolor = "white"
Bordercolor = "# dedfde" borderstyle = "NONE" borderwidth = "1px" cellpadding = "4"
Performanceid = "objectperformance1" forecolor = "black" gridlines = "vertical">
<Rowstyle backcolor = "# f7f7de"/>
<Footerstyle backcolor = "# cccc99"/>
<Pagerstyle backcolor = "# f7f7de" forecolor = "black" horizontalalign = "right"/>
<Selectedrowstyle backcolor = "# ce5d5a" font-bold = "true" forecolor = "white"/>
<Headerstyle backcolor = "# 6b696b" font-bold = "true" forecolor = "white"/>
<Alternatingrowstyle backcolor = "white"/>
</ASP: gridview>

<Asp: objectdatasource id = "objectperformance1" runat = "server" typename = "stuwcfservice. wcfstudenttext" selectmethod = "getstudent">
</ASP: objectdatasource>
</Div>

The advantage of objectsponse is that you can call methods in the proxy class without writing a line of code. It should be noted that typename and selectmethod must be written in the same way as the proxy class.

2.use svcutil.exe

Wcfhas a tool svcutil.exe, which automatically downloads the WSDL document from the specified URL, generates and saves a specified file for the proxy class, generates the corresponding configuration file with the corresponding configuration settings, and executes the following command:

The svcutil.exe tool automatically completes the following tasks:

(1) download the metadata document (WSDL document) through URL ).

(2) generate code for the proxy class and keep the code in the wcfstudentserviceclient. CS file.

(3) generate configuration settings and save them to Web. config.

Check the svcutil.exe multi-run directory and you will see the wcfstudentserviceclient. CS and web. config files. The code in the file is not tested here. You can try it on your own. Import these two files to a site.

You only need to change the code of objectdatasource:

<Asp: objectdatasource id = "objectperformance1" runat = "server" typename = "proxy class name" selectmethod = "called method name">
</ASP: objectdatasource>

In this way, you can.

3. programming scheme

In this case, we want to compile the file generated by svcutil.exe and manually write our own code to achieve the same effect. The agent file generated by svcutil.exe contains five constructors to meet the following requirements:

Code
[System. codedom. compiler. generatedcodeattribute ("system. servicemodel", "3.0.0.0")]
Public partial class stuservicecontractclient: system. servicemodel. clientbase <istuservicecontract>, istuservicecontract
{

Public stuservicecontractclient () // default constructor
{
}

Public stuservicecontractclient (string endpointconfigurationname ):
Base (endpointconfigurationname) // The parameter is a string, including the endpoint configuration name.
{
}

Public stuservicecontractclient (string endpointconfigurationname, string remoteaddress ):
Base (endpointconfigurationname, remoteaddress) // configure the endpoint and network address of the endpoint
{
}

Public stuservicecontractclient (string endpointconfigurationname, system. servicemodel. endpointaddress remoteaddress ):
Base (endpointconfigurationname, remoteaddress) // configure the endpoint and the endpoint object of the endpoint address
{
}

Public stuservicecontractclient (system. servicemodel. channels. Binding binding, system. servicemodel. endpointaddress remoteaddress ):
Base (binding, remoteaddress) // binding object bound to the endpoint, endpoint address object of the endpoint

{
}

Public void addstudent (wcfstudent. Student Stu)
{
Base. Channel. addstudent (Stu );
}

Public wcfstudent. Student [] getstudent ()
{
Return base. Channel. getstudent ();
}
}

The endpoint configuration name is the name attribute under the <client> <endpoint> sub-element.

You can also write the same code. To implement the application of the WCF Service. Write it here.

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.