寄宿WCF服务相关实现方法解析

开发 开发工具
如果我们想要在windows中进行寄宿WCF服务的话,应该如何正确操作才能实现这一功能呢?在这里就会为大家详细介绍相关方法。

WCF是一款由微软公司研发的.NET Framework 3.5重要组成部件。它的功能优势突出,比如在兼容性,安全性方面尤其突出。在windows服务中寄宿WCF服务,需要继承ServiceBase,此外,还须要继承Installer以安装服务.以下为具体实现步骤:

寄宿WCF服务步骤1.创建文件winservice.cs,输入代码

namespace windowswcfservice  
{  
using System.ServiceProcess;  
using System.ServiceModel;  
using System.Configuration.Install;  
using System.Configuration;  
using System.ComponentModel;  
[ServiceContract(Namespace="http://mysample")]  
public interface ICalculator  
{  
[OperationContract]  
double Add(double n1, double n2);  
[OperationContract]  
double Subtract(double n1, double n2);  
[OperationContract]  
double Multiply(double n1, double n2);  
[OperationContract]  
double Divide(double n1, double n2);  
}  
public class CalculatorService : ICalculator  
{  
public double Add(double n1, double n2)  
{  
return n1 + n2;  
}  
public double Subtract(double n1, double n2)  
{  
return n1 - n2;  
}  
public double Multiply(double n1, double n2)  
{  
return n1 * n2;  
}  
public double Divide(double n1, double n2)  
{  
return n1 / n2;  
}  
}  
[RunInstaller(true)]  
public class ProjectInstaller : Installer  
{  
private ServiceProcessInstaller process;  
private ServiceInstaller service;  
public ProjectInstaller()  
{  
process = new ServiceProcessInstaller();  
process.Account = ServiceAccount.LocalSystem;  
service = new ServiceInstaller();  
service.ServiceName = "WCFWindowsServiceSample";  
Installers.Add(process);  
Installers.Add(service);  
}  
}  
public class WindowsCalculatorService : ServiceBase  
{  
public ServiceHost serviceHost = null;  
public static void Main()  
{  
ServiceBase.Run(new WindowsCalculatorService());  
}  
protected override void OnStart(string[] args)  
{  
if (serviceHost != null)  
{  
serviceHost.Close();  
}  
try  
{  
serviceHost = new ServiceHost(typeof(CalculatorService));  
serviceHost.Open();  
}  
catch(System.Exception err)  
{  
System.Diagnostics.EventLog.WriteEntry("Application", err.Message);  
}  
}  
protected override void OnStop()  
{  
if (serviceHost != null)  
{  
serviceHost.Close();  
serviceHost = null;  
}  
}  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.

 

寄宿WCF服务步骤2.用csc编译文件winservice.cs

csc /t:exe winservice.cs /r:"C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Communication

Foundation\System.ServiceModel.dll" /r:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.ServiceProcess.dll /r:System.Configuration.Install.dll

寄宿WCF服务步骤3.创建winservice.exe.config,内容如下:

< ?xml version="1.0"?> 
< configuration> 
< system.serviceModel> 
< services> 
< service name="windowswcfservice.CalculatorService" 
behaviorConfiguration="Metadata" >  < host>  < baseAddresses>  < add baseAddress="http://localhost:8000/service"/>  < /baseAddresses>  < /host>  < endpoint address="*" binding="wsHttpBinding" 
contract="windowswcfservice.ICalculator" />  < endpoint address="mex" binding="mexHttpBinding" 
contract="IMetadataExchange"/>  < /service>  < /services>  < behaviors>  < serviceBehaviors>  < behavior name="Metadata">  < serviceMetadata httpGetEnabled="true" />  < /behavior>  < /serviceBehaviors>  < /behaviors>  < /system.serviceModel>  < /configuration> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

寄宿WCF服务步骤4.利用工具C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe安装windows 服务.installutil.exe winservice.exe

#t#

寄宿WCF服务步骤5.启动服务:net start WCFWindowsServiceSample。如果服务不能启动,可查看事件日志以定位错误.

寄宿WCF服务步骤6.访问wcf服务:http://localhost:8000/service

寄宿WCF服务步骤7.停止wcf服务:net stop WCFWindowsServiceSample

寄宿WCF服务步骤8.卸载windows服务:installutil.exe /u winservice.exe

责任编辑:曹凯 来源: CSDN
相关推荐

2010-02-26 14:39:27

WCF服务寄宿

2010-02-24 11:22:04

WCF方法重载

2010-02-26 17:51:16

Silverlight

2010-02-26 08:59:10

WCF服务宿主程序

2010-02-23 17:30:41

WCF部署于IIS

2010-02-26 16:20:56

WCF程序事务

2010-02-24 14:59:52

WCF自定义过滤器

2010-02-23 10:15:22

WCF运行机制

2009-12-21 11:19:50

WCF配置文件

2010-02-25 13:54:48

WCF安全参数

2010-02-25 16:52:12

引用WCF服务

2010-03-01 16:31:58

WCF实现SOA

2009-12-21 18:10:50

WCF实现事件通知

2010-02-22 17:43:19

WCF服务启动

2009-12-21 17:48:30

WCF方法重载

2010-02-22 14:28:35

WCF实现loadin

2010-02-25 16:20:02

WCF客户端

2010-03-01 13:17:46

WCF单向服务

2009-12-21 13:27:45

WCF服务配置信息

2010-02-25 17:57:26

WCF服务合同
点赞
收藏

51CTO技术栈公众号