宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)
2012-08-28 17:36
453 查看
1. 创建WCF服务
在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1)
IService1接口如下:
Service1实现类如下:
2.创建Window Service ,把WCF服务放在window Service中
先在window Service中添加引用,在对话框中选择Projects->Solution然后将wcfservice引入,这就在windows service中引用wcfservice里的service1时就不会报错了。
增加app.config文件
增加安装服务类。
在服务类的设计面板上,点鼠标右键,然后在弹出的菜单上,点add installer项,然后一个叫ProjectInstaller类增加成功。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
3. 安装或卸载Windows 服务
在windows service上生成解决方案,得到exe
管理员身份运行vs2010的命令行,在exe所在目录执行installutil xxxx.exe
在服务管理中启动baowg服务
4. [b]客户端调用WCF服务[/b]
把baowg服务启动后,给Client项目增加服务引用。输入服务地址http://localhost:8999/Baowg,也就是第一步中配置文件中的地址。
自动生成配置文件app.config
在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1)
IService1接口如下:
using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace WcfService { [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); } // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } }
Service1实现类如下:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace WcfService { public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } }
2.创建Window Service ,把WCF服务放在window Service中
先在window Service中添加引用,在对话框中选择Projects->Solution然后将wcfservice引入,这就在windows service中引用wcfservice里的service1时就不会报错了。
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 System.ServiceModel; using WcfService; namespace WindowsServiceDemo { public partial class Baowg : ServiceBase { private ServiceHost host; public Baowg() { InitializeComponent(); } protected override void OnStart(string[] args) { if (this.host != null) { this.host.Close(); } this.host = new ServiceHost(typeof(WcfService.Service1)); this.host.Open(); } protected override void OnStop() { if (this.host != null) { this.host.Close(); } } } }
增加app.config文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WcfService.Service1" behaviorConfiguration="basicBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8999/Baowg"/> <!--windows service的地址--> </baseAddresses> </host> <!--wcfservice的地址--> <endpoint address="http://localhost:8999/Service1" contract="WcfService.IService1" binding="basicHttpBinding" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="basicBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
增加安装服务类。
在服务类的设计面板上,点鼠标右键,然后在弹出的菜单上,点add installer项,然后一个叫ProjectInstaller类增加成功。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
3. 安装或卸载Windows 服务
在windows service上生成解决方案,得到exe
管理员身份运行vs2010的命令行,在exe所在目录执行installutil xxxx.exe
在服务管理中启动baowg服务
4. [b]客户端调用WCF服务[/b]
把baowg服务启动后,给Client项目增加服务引用。输入服务地址http://localhost:8999/Baowg,也就是第一步中配置文件中的地址。
自动生成配置文件app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:8999/Service1" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /> </client> </system.serviceModel> </configuration>
相关文章推荐
- 宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)
- WCF宿主在IIS, Application, WindowsService, WAS
- BizTalk Orchestration Publish Host In-Process Wcf Service without IIS 多种供客户端调用方式
- NetCore Selfhost,IIShost,Windows Service Host详解(自宿主、宿主在IIS,宿主在Windows Service中)
- 化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
- 化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
- 化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
- BizTalk Orchestration Publish Host In-Process Wcf Service without IIS 多种供客户端调用方式
- WCF实例 —— Windows Service Host 开发,调试,制作安装包
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
- 安装IIS 7以后无法启动依赖服务windows process activation service
- C# VS 2010创建、安装、调试 windows服务(windows service)
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
- WCF 服务 宿主在windows service中的Demo
- 综合应用WPF/WCF/WF/LINQ之五:将Workflow使用WCF技术Host到IIS中,并实现调用
- VS 2010中使用C#创建及调用WCF完整实例(Windows服务宿主)
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
- 利用IIS作为宿主 发布你的WCF Service(转)
- 使用C#代码安装、启动、停止、卸载Windows service(不使用InstallUtil.exe)
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)