用wcf建立一个简单的windows服务并生成服务
- 打开Visual Studio,选择“新建项目”。
- 在“新建项目”对话框中,选择“Windows服务”模板。
- 输入项目名称,选择项目保存路径,点击“创建”按钮。
- 在“Solution Explorer”中,右击“项目名称”并选择“添加” -> “新建项”。
- 在“添加新项”对话框中,选择“WCF服务”模板。
- 输入WCF服务名称,选择服务保存路径,点击“添加”按钮。
- 在“Solution Explorer”中,右击WCF服务文件并选择“显示WCF服务代码”。
- 在WCF服务代码中,添加以下代码:
using System.ServiceModel;
[ServiceContract]
public interface IService
{
[OperationContract]
string HelloWorld();
}
public class Service : IService
{
public string HelloWorld()
{
return "Hello, World!";
}
}
- 在WCF服务代码中,添加以下代码以便在服务启动时启动WCF服务:
using System.ServiceModel;
public partial class Service : ServiceBase
{
private ServiceHost serviceHost = null;
public Service()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
serviceHost = new ServiceHost(typeof(Service));
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
- 在WCF服务代码中,添加以下代码以便生成服务元数据:
using System.ServiceModel.Description;
public partial class Service : ServiceBase
{
// ...
protected override void OnStart(string[] args)
{
// ...
ServiceMetadataBehavior metadataBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
serviceHost.Description.Behaviors.Add(metadataBehavior);
}
metadataBehavior.HttpGetEnabled = true;
metadataBehavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
}
// ...
}
- 在WCF服务代码中,添加以下代码以便启用基础HTTP绑定:
using System.ServiceModel.Web;
public partial class Service : ServiceBase
{
// ...
protected override void OnStart(string[] args)
{
// ...
serviceHost.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "http://localhost:8080/Service");
// ...
}
// ...
}
- 在WCF服务代码中,添加以下代码以便在服务启动时启动日志记录:
using System.Diagnostics;
public partial class Service : ServiceBase
{
// ...
protected override void OnStart(string[] args)
{
// ...
EventLog eventLog = new EventLog();
if (!EventLog.SourceExists("WCF Service"))
{
EventLog.CreateEventSource("WCF Service", "Application");
}
eventLog.Source = "WCF Service";
eventLog.Log = "Application";
eventLog.WriteEntry("Service started.");
// ...
}
// ...
}
- 在WCF服务代码中,添加以下代码以便在服务停止时停止日志记录:
using System.Diagnostics;
public partial class Service : ServiceBase
{
// ...
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
EventLog eventLog = new EventLog();
eventLog.Source = "WCF Service";
eventLog.Log = "Application";
eventLog.WriteEntry("Service stopped.");
// ...
}
// ...
}
- 在WCF服务代码中,添加以下代码以便安装服务:
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess;
static class Program
{
static void Main()
{
if (Environment.UserInteractive)
{
string[] arguments = Environment.GetCommandLineArgs();
if (arguments.Length > 1)
{
switch (arguments[1])
{
case "install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service()
};
ServiceBase.Run(ServicesToRun);
}
}
}
- 在WCF服务代码中,添加以下代码以便卸载服务:
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess;
static class Program
{
// ...
static void Main()
{
if (Environment.UserInteractive)
{
string[] arguments = Environment.GetCommandLineArgs();
if (arguments.Length > 1)
{
switch (arguments[1])
{
case "install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
}
else
{
// ...
}
}
}
- 在“Solution Explorer”中,右击“项目名称”并选择“属性”。
- 在“属性页”中,选择“调试”选项卡。
- 在“启动操作”中,选择“外部程序”并输入“C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe”。
- 在“命令行参数”中输入“$(TargetPath)”。
- 点击“确定”按钮以保存设置。
- 在“Solution Explorer”中,右击“项目名称”并选择“生成” -> “生成解决方案”。
- 打开“命令提示符”并输入“sc create WCFService binPath= "C:...\WCFService.exe"”,其中“C:...\WCFService.exe”为WCF服务的路径。
- 打开“服务”管理器并启动“WCF Service”服务。
- 打开浏览器并访问“http://localhost:8080/Service?wsdl”以访问服务元数据。
- 打开“WCF Test Client”并添加服务引用。
- 在“WCF Test Client”中,选择服务方法并点击“调用”按钮,以调用服务方法并显示返回值
原文地址: https://www.cveoy.top/t/topic/ho55 著作权归作者所有。请勿转载和采集!