WCF uses code-only methods for service boarding, and wcf code-based boarding
The purpose of service hosting is to enable a process and provide a running environment for the WCF Service. By adding one or more endpoints to a service to expose it to potential service consumption, the service consumer calls the service by matching the endpoints, except for the preceding two boarding methods, you can also implement service boarding in the form of pure code.
Create a console application and add a reference to the System. ServiceModel library file.
Add a WCF Service Interface: ISchool uses ServiceContract for interface constraints, and OperationContract for behavior constraints
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.ServiceModel.Activation;namespace WcfTest.Demo3{ [ServiceContract] public interface ISchool { [OperationContract] string ShowName(string Name); }}
Add the School class to implement the Ishool interface:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WcfTest.Demo3{ public class School:ISchool { public string ShowName(string name) { return "Show Name " + name; } }}
Use the code to host the ISchool interface to the console application.
Class Program {static void Main (string [] args) {CoreCodeOpen ();} /// <summary> /// use code to host the service. // </summary> static void CoreCodeOpen () {try {using (ServiceHost host = new ServiceHost (typeof (School) {host. addServiceEndpoint (typeof (ISchool), new WSHttpBinding ()," http://10.0.0.217:20004/School "); If (host. description. behaviors. find <ServiceMetadataBehavior> () = null) {ServiceMetadataBehavior behavior = new ServiceMetadataBehavior (); behavior. httpGetEnabled = true; behavior. httpGetUrl = new Uri (" http://10.0.0.217:20004/School /Wcfapi "); host. description. behaviors. add (behavior);} host. open (); Console. writeLine ("host to the Console application, enable the WCF Service by Code"); Console. readKey () ;}} catch (Exception ex) {Console. writeLine ("CoreCodeOpen Error:" + ex. message );}}}
Run the program as an administrator> enter http: // 10.0.0.217: 20004/School/wcfapi in the browser> we will get the WSDL (Web Services Description Language) service metadata
Create a console client in solution, add service reference, and enter http: // 10.0.0.217: 20004/School/wcfapi
Find our Ischool service interface and call the service after adding the reference.