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