Source文件
-------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ComponentModel; 7 using System.ServiceModel; 8 using System.ServicePRocess; 9 using System.Configuration; 10 using System.Configuration.Install; 11 12 namespace Microsoft.ServiceModel.Samples 13 { 14 // Define a service contract. 15 [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] 16 public interface ICalculator 17 { 18 [OperationContract] 19 double Add(double n1, double n2); 20 [OperationContract] 21 double Subtract(double n1, double n2); 22 [OperationContract] 23 double Multiply(double n1, double n2); 24 [OperationContract] 25 double Divide(double n1, double n2); 26 } 27 28 // Implement the ICalculator service contract in a service class. 29 public class CalculatorService : ICalculator 30 { 31 // Implement the ICalculator methods. 32 public double Add(double n1, double n2) 33 { 34 double result = n1 + n2; 35 return result; 36 } 37 38 public double Subtract(double n1, double n2) 39 { 40 double result = n1 - n2; 41 return result; 42 } 43 44 public double Multiply(double n1, double n2) 45 { 46 double result = n1 * n2; 47 return result; 48 } 49 50 public double Divide(double n1, double n2) 51 { 52 double result = n1 / n2; 53 return result; 54 } 55 } 56 57 public class CalculatorWindowsService : ServiceBase 58 { 59 public ServiceHost serviceHost = null; 60 public CalculatorWindowsService() 61 { 62 // Name the Windows Service 63 ServiceName = "WCFWindowsServiceSample"; 64 } 65 66 public static void Main() 67 { 68 ServiceBase.Run(new CalculatorWindowsService()); 69 } 70 71 // Start the Windows service. 72 protected override void OnStart(string[] args) 73 { 74 if (serviceHost != null) 75 { 76 serviceHost.Close(); 77 } 78 79 // Create a ServiceHost for the CalculatorService type and 80 // provide the base address. 81 serviceHost = new ServiceHost(typeof(CalculatorService)); 82 83 // Open the ServiceHostBase to create listeners and start 84 // listening for messages. 85 serviceHost.Open(); 86 } 87 88 protected override void OnStop() 89 { 90 if (serviceHost != null) 91 { 92 serviceHost.Close(); 93 serviceHost = null; 94 } 95 } 96 } 97 98 // Provide the ProjectInstaller class which allows 99 // the service to be installed by the Installutil.exe tool100 [RunInstaller(true)]101 public class ProjectInstaller : Installer102 {103 private ServiceProcessInstaller process;104 private ServiceInstaller service;105 106 public ProjectInstaller()107 {108 process = new ServiceProcessInstaller();109 process.Account = ServiceAccount.LocalSystem;110 service = new ServiceInstaller();111 service.ServiceName = "WCFWindowsServiceSample";112 Installers.Add(process);113 Installers.Add(service);114 }115 }116 }
app.config
-----------------------------
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service behaviorConfiguration="CalculatorServiceBehavior" name="Microsoft.ServiceModel.Samples.CalculatorService"> <endpoint address="http://localhost:8000/ServiceModelSamples/service" behaviorConfiguration="NewBehavior0" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <endpoint address="http://localhost:8000/ServiceModelSamples/service/mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8000/ServiceModelSamples/service" /> </baseAddresses> </host> </service> </services> <behaviors> <endpointBehaviors> <behavior name="NewBehavior0" /> </endpointBehaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8000/Sample" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
新闻热点
疑难解答