(1)创造WCF服务应用程序

(1)创建WCF

(2)配置IIS

代码示例:

将WCF服务应用程序配置IIS网址,需求使用.net4.0集成版本的程序池

[ServiceContract]

(3)使用SvcUtil.exe生成客商端代码和配备

public interface ILimsDBService

  SvcUtil.exe是二个VS命令行工具,该工具位于:C:\Program
Files\Microsoft  SDKs\Windows\v7.0A\bin 或 C:\Program Files
(x86)\Microsoft
SDKs\Windows\v7.0A\bin\相似意况下我们将SvcUtil.exe加多到VS开垦工具中有益以往的运用(也可直接使用该命令行工具)。在VS中的
Tools菜单—选用External
Tools,展开管理窗口。在Title中输入SvcUtil,Command中选拔SvcUtil.exe全路径,Initial 
directory栏选拔生成的顾客端代码和配置文件所放的目录(此处为解决方案所在目录),选上Prompt
for arguments,不选上Close on 
exit。点击OK.增添完毕后,在VS的工具下会鬼使神差这一个菜单。在Client端增多对服务的援用。张开SvUtil工具,在Arguments里填充服务的地点,点击OK。此时期理类和配置文件被下载到施工方案的情理目录中。

{

(4)配置客户端代码

[OperationContract]

WSHttpBinding binding = new WSHttpBinding();

int ExecuteSql(string strSql); 

EndpointAddress address = new
EndpointAddress(“http://192.168.4.179:8888/LimsDBService.svc“);

[OperationContract]

LimsDBServiceClient lims = new LimsDBServiceClient(binding, address);

DataTable GetDataTable(string strSql); 

lims.Open();

[OperationContract]

string sql = “select sample_id,material_type from nais_all_samples
where sample_id=” + this.txtQuery.Text.Trim();

DataSet GetDataSet(string strSql);

DataTable dt = lims.GetDataSet(sql).Tables[0];

}

lims.Close();

public class LimsDBService : ILimsDBService

this.gvData.DataSource = dt;

{

this.gvData.DataBind();

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();

相关文章