Create an empty solution in VS
1. First create a class library to define the service contract
Add Reference: "System.ServiceModel"
HelloService:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.ServiceModel;namespacehelloservice{ Public classHelloservice:ihelloservice {/// <summary> ///Greeting/// </summary> /// <param name= "name" ></param> /// <returns></returns> Public stringSayHello (stringname) { returnName +": Hello"; } }}View Code
Ihelloservice:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.ServiceModel;namespacehelloservice{/// <summary> ///Service Contract/// </summary>[ServiceContract] Public InterfaceIhelloservice {/// <summary> ///Service Operations/// </summary> /// <param name= "name" ></param> /// <returns></returns>[OperationContract]stringSayHello (stringname); }}View Code
2. Create a console Application
Add Reference: "System.ServiceModel"
Add Project reference, reference project "HelloService"
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.ServiceModel;usingSystem.ServiceModel.Channels;namespacehelloservicehost{classProgram {Static voidMain (string[] args) { using(Myhellohost Host =NewMyhellohost ()) {Host.open (); Console.read (); } } } Public classmyhellohost:idisposable {/// <summary> ///Define a service object/// </summary> PrivateServiceHost _myhost; /// <summary> ///define a base address/// </summary> Public Const stringBaseAddress ="Net.pipe://localhost"; /// <summary> ///Optional Address/// </summary> Public Const stringHelloserviceaddress ="Hello"; //Service Contract Implementation type Public Static ReadOnlyType servicetype=typeof(Helloservice.helloservice); //Service Contract Interface Public Static ReadOnlyType Contracttype =typeof(Helloservice.ihelloservice); /// <summary> ///The service defines a binding/// </summary> Public Static ReadOnlyBinding hellobinding =Newnetnamedpipebinding (); /// <summary> ///Constructing the service object/// </summary> protected voidCreatehelloservicehost () {//Create a service object_myhost =NewServiceHost (ServiceType,NewUri[] {NewUri (baseaddress)}); //To add an end point_myhost. AddServiceEndpoint (Contracttype, hellobinding, helloserviceaddress); } PublicServiceHost Myhost {Get{return_myhost;} } /// <summary> ///Open Service Method/// </summary> Public voidOpen () {Console.WriteLine ("start service started ..."); _myhost. Open (); Console.WriteLine ("Service has been started ..."); } //Construction Method PublicMyhellohost () {createhelloservicehost (); } //destroying Service Host object instances Public voidDispose () {if(_myhost!=NULL) {(_myhost asIDisposable). Dispose (); } } }}View Code
3. Create a console Application
Add Reference: "System.ServiceModel"
Add Project reference, reference project "HelloService"
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.ServiceModel;usingSystem.ServiceModel.Channels;usingHelloService;namespacehelloclient1{classProgram {Static voidMain (string[] args) { using(Helloproxy proxy =NewHelloproxy ()) { //invoking services with proxiesConsole.WriteLine (proxy. Say ("Zhang San")); Console.read (); } } } //hard-coded definition service contract InterfaceIService {//Service Operations[OperationContract] string Say (string name); } classHelloproxy:clientbase<ihelloservice>, IService {//hard-coded definition bindings Public Static ReadOnlyBinding hellobinding =Newnetnamedpipebinding (); //hard-coded definition address Public Static ReadOnlyEndpointAddress helloaddress =NewEndpointAddress (NewUri ("Net.pipe://localhost/hello")); //Construction Method PublicHelloproxy ():Base(hellobinding,helloaddress) {} Publicstring Say (string name) {//invoking a service using the Channel property returnChannel.sayhello (name); } }}View Code
No configuration implementation of WCF calls