No configuration implementation of WCF calls

Source: Internet
Author: User

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

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.