using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using OpcServerHost.Init; using System.ServiceModel; using System.Configuration; using System.ServiceModel.Channels; namespace OpcService { partial class OpcDeal : ServiceBase { public OpcDeal() { InitializeComponent(); } protected override void OnStart(string[] args) { //装配数据工厂 QMFrameWork.Data.DataFactory.Configure(true); ParaInit.InitParameter(); NetTcpBinding bind = new NetTcpBinding(); string serverIP = ConfigurationManager.AppSettings["ServerAddressIP"]; Uri uri = new Uri(serverIP); //从配置文D:\项目文件夹\FJC_CC_B9\truck\B9\ServicesCenter\WCF\OpcServerHost\App.config件中读取服务的Url ServiceHost host = new ServiceHost(typeof(OpcServerHost.OpcService), uri); ////if中的代码可以没有,但是如果想利用Svctuil.exe生成代理类的时候,就需要下面的代码,否则将会报错,无法解析元数据 if (host.Description.Behaviors.Find() == null) { BindingElement metaElement = new TcpTransportBindingElement(); CustomBinding metaBind = new CustomBinding(metaElement); host.Description.Behaviors.Add(new System.ServiceModel.Description.ServiceMetadataBehavior()); host.AddServiceEndpoint(typeof(System.ServiceModel.Description.IMetadataExchange), metaBind, "MEX"); } host.Open(); } protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 } public void TestStart(string[] args) { this.OnStart(args); } } }