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