WCF uses code-only methods for service boarding, and wcf code-based boarding

Source: Internet
Author: User

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.

Related Article

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.