基于Windows服务的WCF

Posted M守护神.NET

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Windows服务的WCF相关的知识,希望对你有一定的参考价值。

(1)创建WCF
代码示例:
[ServiceContract]
public interface ILimsDBService
{
[OperationContract]
int ExecuteSql(string strSql); 
[OperationContract]
DataTable GetDataTable(string strSql); 
[OperationContract]
DataSet GetDataSet(string strSql);
}
public class LimsDBService : ILimsDBService
{
public int ExecuteSql(string strSql)
{
OracleDBHelper dbo = new OracleDBHelper(ConfigInfo.LimsDbConnStr);
return dbo.ExecuteSql(strSql);
public DataTable GetDataTable(string strSql)
{
OracleDBHelper dbo = new OracleDBHelper(ConfigInfo.LimsDbConnStr);
return dbo.GetDataTable(strSql);
public DataSet GetDataSet(string strSql)
{
OracleDBHelper dbo = new OracleDBHelper(ConfigInfo.LimsDbConnStr);
return dbo.GetDataSet(strSql);
}
}
(2)创建Windows服务
代码示例:
public partial class LimsHsSysService : ServiceBase
{
ServiceHost LimsDBServiceHost = new ServiceHost(typeof(LimsDBService));
public LimsHsSysService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
LimsDBServiceHost.Open();
}
protected override void OnStop()
{
LimsDBServiceHost.Close();
}
}
(3)配置客户端
功能代码:
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress address = new EndpointAddress("http://192.168.4.179:8732/Design_Time_Addresses/WcfLimsServer/LimsDBService/");
LimsDBServiceClient lims = new LimsDBService.LimsDBServiceClient(binding, address);
string sql = "select sample_id,material_type from nais_all_samples where sample_id=" + this.txtQuery.Text.Trim();
DataTable dt = lims.GetDataSet(sql).Tables[0];
this.gvData.DataSource = dt;
this.gvData.DataBind();

以上是关于基于Windows服务的WCF的主要内容,如果未能解决你的问题,请参考以下文章

WCF Windows 服务无法启动,抛出 System.IO.FileNotFoundException

基于C#实现Windows服务

Windows下基于IIS服务的SSL服务器的配置

搭建基于Nagios的监控系统——之监控远程Windows服务器

工作组上的 WCF

基于Windows服务的WCF