Two ways that WCF clients invoke service operations

Source: Internet
Author: User

The main content of this section: 1, the service operation is invoked through the proxy class. 2. Invoke the service operation through the channel. 3. Code download

I. Invoking service operations through proxy classes (two ways to add proxy classes)

1. Manually write the proxy class as follows:

Client contract:

 using   System;  using   System.Collections.Generic;  using   System.Linq;  using   System.Text;  using   System.ServiceModel;  namespace   y.wcffirst.client.proxys{[ServiceContract]   Public  interface   Ihello {[Operationco Ntract]  string  Say (string   name); }}
View Code

Client proxy class:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.ServiceModel;usingSystem.ServiceModel.Channels;namespacey.wcffirst.client.proxys{ Public classHelloproxy:clientbase<ihello>, Ihello { Publichelloproxy ():Base()        {        }         Public stringSay (stringname) {            return Base.        Channel.say (name); }    }}
View Code

Client App. Config file:

<?xml version="1.0"encoding="Utf-8"?><configuration> <system.serviceModel> <client> <endpoint name="Wcffirst"Address="Net.tcp://localhost:6666/hello"binding="nettcpbinding"Contract="Y.wcffirst.client.proxys.ihello"></endpoint> </client> </system.serviceModel></configuration>
View Code

Client-side invocation:

using New Helloproxy ()) {                  Console.WriteLine ("Recevie from server:{0}", Proxy. Say (name));                     Proxy. Close ();}
View Code

2. The proxy class is generated by the metadata method.

The server needs to configure App. Config as follows:

Client operation steps: First run the server side (host).

A. Click the Add Service Reference button on the client side to add the service reference. Such as:

B. Enter address: http://localhost:8888, click "GO" to get the service operation. and rename the namespace, such as:

C. The client calls to the proxy class.

using New helloclient ()) {                    Console.WriteLine ("Recevie from server:{0}", Clientproxy.say (name));                     Clientproxy.close ();}
View Code

Second, invoke the service operation through the channel mode

1. Client contract, as follows

 using   System;  using   System.Collections.Generic;  using   System.Linq;  using   System.Text;  using   System.ServiceModel;  namespace   y.wcffirst.clientchannel.proxy{[ServiceContract]  public  interface   Ihello {[Operationc Ontract]  string  Say (string   name); }}
View Code

2. Settings for the client configuration file are as follows:

<?xml version="1.0"encoding="Utf-8"?><configuration> <system.serviceModel> <client> <endpoint name="Wcffirst"Address="Net.tcp://localhost:6666/hello"binding="nettcpbinding"Contract="Y.wcffirst.clientchannel.proxy.ihello"></endpoint> </client> </system.serviceModel></configuration>
View Code

3. Client Invoke service operation: as follows:

New Channelfactory<ihello> ("wcffirst");                 = Factory. CreateChannel ();                 using  as IDisposable)                {                    Console.WriteLine ("Recevie from server:{0}", Channelproxy.say ( name));                }
View Code

code Download

Two ways that WCF clients invoke service operations

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.