当前位置: 首页 > news >正文

网站建设管理 税务黄页网推广服务

网站建设管理 税务,黄页网推广服务,网络运营岗位职责,mvc5 网站开发美学 pdf目录 引言 1. 创建一个新的Windows服务项目 2. 添加WCF服务 2.1 添加服务接口和实现 2.2 添加服务配置 3. 实现Windows服务 3.1 修改Service1类 3.2 在项目中添加ServiceInstaller 4. 安装和运行Windows服务 4.1 编译项目 4.2 使用InstallUtil.exe安装服务 …

目录

引言        

1. 创建一个新的Windows服务项目

2. 添加WCF服务

2.1 添加服务接口和实现

2.2 添加服务配置

3. 实现Windows服务

3.1 修改Service1类

3.2 在项目中添加ServiceInstaller

4. 安装和运行Windows服务

4.1 编译项目

4.2 使用InstallUtil.exe安装服务

4.3 启动服务

5. 测试WCF服务

5.1 添加客户端应用程序

5.2 添加服务引用

5.3 调用服务

6. 卸载Windows服务


引言        

        我们利用C#可以快速开发各种后端服务,如常见的windows服务、WCF 服务、WebService服务、WebApi等,上一篇文章介绍了简单WCF服务的实现,本文将详细讲解如何基于 C# 和 .NET Framework 4.0 将 WCF服务和宿主项目合并到同一个项目中,并将其安装为Windows服务,确保每个步骤都清晰且易于理解和掌握。

以下是详细的步骤:

1. 创建一个新的Windows服务项目

  1. 打开Visual Studio,创建一个新的项目。
  2. 选择 Windows Service 项目类型,命名为 WcfWindowsService

2. 添加WCF服务

2.1 添加服务接口和实现

在项目中添加两个类:IService1.csService1.cs

IService1.cs:

using System.ServiceModel;namespace WcfWindowsService
{[ServiceContract]public interface IService1{[OperationContract]string GetData(int value);}
}

Service1.cs:

namespace WcfWindowsService
{public class Service1 : IService1{public string GetData(int value){return $"You entered: {value}";}}
}
2.2 添加服务配置

在项目中添加一个 App.config 文件,并添加以下配置:

<configuration><system.serviceModel><services><service name="WcfWindowsService.Service1"><endpoint address="" binding="basicHttpBinding" contract="WcfWindowsService.IService1" /><host><baseAddresses><add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfWindowsService/Service1/" /></baseAddresses></host></service></services><behaviors><serviceBehaviors><behavior><serviceMetadata httpGetEnabled="True" /><serviceDebug includeExceptionDetailInFaults="False" /></behavior></serviceBehaviors></behaviors></system.serviceModel>
</configuration>

3. 实现Windows服务

3.1 修改Service1类

Service1.cs 文件中创建一个新的类 WcfService,继承自 ServiceBase 类:

using System;
using System.ServiceModel;
using System.ServiceProcess;namespace WcfWindowsService
{public partial class WcfService : ServiceBase{public ServiceHost serviceHost = null;public WcfService(){InitializeComponent();}protected override void OnStart(string[] args){if (serviceHost != null){serviceHost.Close();}serviceHost = new ServiceHost(typeof(Service1));serviceHost.Open();}protected override void OnStop(){if (serviceHost != null){serviceHost.Close();serviceHost = null;}}}
}
3.2 在项目中添加ServiceInstaller
  1. 右键点击项目,选择 Add -> New Item
  2. 选择 Component Class,命名为 ProjectInstaller.cs

ProjectInstaller.cs 中实现服务安装:

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;namespace WcfWindowsService
{[RunInstaller(true)]public partial class ProjectInstaller : Installer{private ServiceProcessInstaller processInstaller;private ServiceInstaller serviceInstaller;public ProjectInstaller(){processInstaller = new ServiceProcessInstaller();serviceInstaller = new ServiceInstaller();// 设置服务使用的账号类型processInstaller.Account = ServiceAccount.LocalSystem;// 设置服务信息serviceInstaller.ServiceName = "WcfWindowsService";serviceInstaller.DisplayName = "WCF Windows Service";serviceInstaller.StartType = ServiceStartMode.Manual;Installers.Add(processInstaller);Installers.Add(serviceInstaller);}}
}

4. 安装和运行Windows服务

4.1 编译项目

确保项目没有错误,然后编译项目。

4.2 使用InstallUtil.exe安装服务

打开管理员权限的命令提示符,导航到编译输出目录(通常是bin\Debug或bin\Release),并运行以下命令以安装服务:

installutil WcfWindowsService.exe
4.3 启动服务
  1. 打开 Services 管理器(按下Win+R,输入 services.msc 后回车)。
  2. 找到名为 WcfWindowsService 的服务。
  3. 右键点击服务,选择 Start 启动服务。

5. 测试WCF服务

5.1 添加客户端应用程序
  1. 创建一个新的 Console Application 项目来测试服务,命名为 WcfClient
5.2 添加服务引用

在客户端项目中,右键点击项目,选择 Add Service Reference,输入服务地址(如 http://localhost:8733/Design_Time_Addresses/WcfWindowsService/Service1/),然后点击 GoOK

5.3 调用服务

在客户端的 Program.cs 中调用服务:

using System;namespace WcfClient
{class Program{static void Main(string[] args){var client = new ServiceReference1.Service1Client();string result = client.GetData(42);Console.WriteLine(result);client.Close();}}
}

6. 卸载Windows服务

如果不再需要服务,可以卸载:

installutil /u WcfWindowsService.exe

        通过这些步骤,您可以将WCF服务和宿主项目合并到同一个项目中,并将其安装为Windows服务。这样可以实现WCF服务的长期运行,并通过Windows服务管理器来管理服务的生命周期。

http://www.bdqn1.cn/news/490.html

相关文章:

  • 福建工程网站建设团队个人网站推广平台大全
  • 国家重大新闻新seo排名点击软件
  • 泰安房产网签西湖春晓seo排名
  • 部门子网站建设方案外链管理
  • seo 网站关键词百度seo收录软件
  • 电子书网站 跟我学做家常菜800友情链接可以帮助店铺提高浏览量
  • wap网站和internet网站怎么快速优化网站排名
  • 网页 网站十大管理培训课程
  • 云南人社上海知名的seo推广咨询
  • b2b网站大全前十名品牌推广方案范文
  • 在线黑科技网站长沙岳麓区
  • 北京集团网站建设公司最好的网站设计公司
  • 网站做seo外链seo关键词分析
  • 做服装要看国外哪些网站好广告公司接单软件
  • 网站的倒计时怎么做网络广告策划方案
  • 人与畜禽狗croproation伟哥seo博客
  • 编辑网站的软件手机世界搜索引擎公司排名
  • 宁波网站优化方案网站建设的技术支持
  • 自建公司网站广州seo团队
  • 帮忙做网站的协议网站制作软件
  • 杭州行业网站建设直通车怎么开才有效果
  • 网上做批发网站有哪些网络营销的特点有哪些?
  • 济南助企网站建设公司怎么样下载百度卫星导航
  • 做网站怎样上传文件信息流优化师证书
  • 平顶山城市住房城乡建筑网站百度手机版
  • 建手机网站软件长沙做网站的公司有哪些
  • 专门做珠宝的网站自建站
  • 网站如何做二级域名舆情系统
  • 露天做愛偷拍网站最佳的资源搜索引擎
  • 网站建设架构书找客源免费用哪个软件好