WCF uses Windows services as host

Source: Internet
Author: User

ICalculator.cs
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; namespace windowsservice1{    [ServiceContract]    publicinterface  ICalculator    {        [OperationContract]        double Add (doubledouble  n2);}    } 

CalculatorService.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.ServiceModel;namespacewindowsservice1{//Implement The ICalculator service contract in a service class. (Instancecontextmode=instancecontextmode.single)[ServiceBehavior] Public classCalculatorService:WindowsService1.ICalculator {//Implement the ICalculator methods.         Public DoubleADD (DoubleN1,Doublen2) {            Doubleresult = N1 +N2; returnresult; }    }}

CalculatorWindowsService.cs
usingSystem;usingSystem.Linq;usingSystem.ServiceModel;usingsystem.serviceprocess;usingSystem.IO;namespacewindowsservice1{ Public Partial classCalculatorwindowsservice:servicebase { PublicServiceHost ServiceHost =NULL;  PublicCalculatorwindowsservice () {InitializeComponent (); }        protected Override voidOnStart (string[] args) {            if(ServiceHost! =NULL) {servicehost.close (); } serviceHost=NewServiceHost (typeof(calculatorservice));        Servicehost.open (); }                protected Override voidOnStop () {if(ServiceHost! =NULL) {servicehost.close (); ServiceHost=NULL; }        }    }}

App.

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <System.ServiceModel>    <Services>      <Servicebehaviorconfiguration= "Basicbehavior"name= "Windowsservice1.calculatorservice">        <EndpointAddress=""binding= "NetTcpBinding"bindingconfiguration=""Contract= "Windowsservice1.icalculator" />        <EndpointAddress= "Mex"binding= "Mextcpbinding"bindingconfiguration=""Contract= "IMetadataExchange" />        <Host>          <baseaddresses>            <Addbaseaddress= "Net.tcp://127.0.0.1:9999/calculatorservice" />          </baseaddresses>        </Host>      </Service>    </Services>    <Behaviors>      <servicebehaviors>        <Behaviorname= "Basicbehavior">          <Servicemetadatahttpgetenabled= "true"Httpgeturl= "Http://127.0.0.1:9998/CalculatorService/metadata"httpsgetenabled= "true" />          <ServicedebugIncludeexceptiondetailinfaults= "true" />        </Behavior>      </servicebehaviors>    </Behaviors>  </System.ServiceModel></Configuration>

You can remove app. Config and use code to configure it directly

protected Override voidOnStart (string[] args) {    if(ServiceHost! =NULL) {servicehost.close (); }    Try{serviceHost=NewServiceHost (typeof(Windowsservice1.calculatorservice),NewUri ("Http://127.0.0.1:9999/CalculatorService") ); ServiceMetadataBehavior SMB= servicehost.description.behaviors.find<servicemetadatabehavior>(); if(SMB = =NULL) {SMB=NewServiceMetadataBehavior (); } SMB. Httpgetenabled=true; Smb. Metadataexporter.policyversion=policyversion.policy15;        SERVICEHOST.DESCRIPTION.BEHAVIORS.ADD (SMB); Servicehost.addserviceendpoint (Servicemetadatabehavior.mexcontractname, METADATAEXCHANGEBINDINGS.CREATEMEXHTTPB Inding (),"Mex"); Servicehost.addserviceendpoint (typeof(ICalculator),NewWshttpbinding (),"");    Servicehost.open (); }    Catch(Exception ex) {}}

1. Test Tool using WcfTestClient

2. Client testing:

usingSystem;usingSystem.ServiceModel;usingWindowsService1;namespacetestwindowsserviceclient{classProgram {Static voidMain (string[] args) {            /*//http ICalculator client = Channelfactory<icalculator>. CreateChannel (New Wshttpbinding (), New EndpointAddress ("http://127.0.0.1: 9999/calculatorservice ")); */            //TCPICalculator client = channelfactory<icalculator>. CreateChannel (Newnettcpbinding (),NewEndpointAddress ("Net.tcp://127.0.0.1:9999/calculatorservice")); DoubleRET = client. ADD (23.0,2.0); Console.WriteLine ("Add Result: {0}", ret);        Console.read (); }    }}

WCF uses Windows services as host

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.