宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)

5 篇文章 0 订阅
4 篇文章 0 订阅
1. 创建WCF服务

 在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1)
 IService1接口如下:


 
 
[csharp] view plain copy
  1. using System.Runtime.Serialization;  
  2. using System.ServiceModel;  
  3. using System.ServiceModel.Web;  
  4. using System.Text;  
  5. namespace WcfService  
  6. {  
  7.     [ServiceContract]  
  8.     public interface IService1  
  9.     {  
  10.         [OperationContract]  
  11.         string GetData(int value);  
  12.   
  13.         [OperationContract]  
  14.         CompositeType GetDataUsingDataContract(CompositeType composite);  
  15.     }  
  16.     // 使用下面示例中说明的数据约定将复合类型添加到服务操作。  
  17.     [DataContract]  
  18.     public class CompositeType  
  19.     {  
  20.         bool boolValue = true;  
  21.         string stringValue = "Hello ";  
  22.   
  23.         [DataMember]  
  24.         public bool BoolValue  
  25.         {  
  26.             get { return boolValue; }  
  27.             set { boolValue = value; }  
  28.         }  
  29.   
  30.         [DataMember]  
  31.         public string StringValue  
  32.         {  
  33.             get { return stringValue; }  
  34.             set { stringValue = value; }  
  35.         }  
  36.     }  
  37. }  
Service1实现类如下:
[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.   
  9. namespace WcfService  
  10. {  
  11.     public class Service1 : IService1  
  12.     {  
  13.         public string GetData(int value)  
  14.         {  
  15.             return string.Format("You entered: {0}", value);  
  16.         }  
  17.   
  18.         public CompositeType GetDataUsingDataContract(CompositeType composite)  
  19.         {  
  20.             if (composite == null)  
  21.             {  
  22.                 throw new ArgumentNullException("composite");  
  23.             }  
  24.             if (composite.BoolValue)  
  25.             {  
  26.                 composite.StringValue += "Suffix";  
  27.             }  
  28.             return composite;  
  29.         }  
  30.     }  
  31. }  

2.创建Window Service ,把WCF服务放在window Service中

先在window Service中添加引用,在对话框中选择Projects->Solution然后将wcfservice引入,这就在windows service中引用wcfservice里的service1时就不会报错了。

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.Linq;  
  7. using System.ServiceProcess;  
  8. using System.Text;  
  9. using System.ServiceModel;  
  10. using WcfService;  
  11.   
  12. namespace WindowsServiceDemo  
  13. {  
  14.     public partial class Baowg : ServiceBase  
  15.     {  
  16.   
  17.         private ServiceHost host;  
  18.   
  19.         public Baowg()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.         protected override void OnStart(string[] args)  
  25.         {  
  26.             if (this.host != null)  
  27.             {  
  28.                 this.host.Close();  
  29.             }  
  30.             this.host = new ServiceHost(typeof(WcfService.Service1));  
  31.             this.host.Open();  
  32.   
  33.         }  
  34.   
  35.         protected override void OnStop()  
  36.         {  
  37.             if (this.host != null)  
  38.             {  
  39.                 this.host.Close();  
  40.             }  
  41.         }  
  42.     }  
  43. }  

增加app.config文件
 
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <services>  
  5.       <service name="WcfService.Service1" behaviorConfiguration="basicBehavior">  
  6.         <host>  
  7.           <baseAddresses>  
  8.             <add baseAddress="http://localhost:8999/Baowg"/> <!--windows service的地址-->  
  9.           </baseAddresses>  
  10.         </host>  
  11.         <!--wcfservice的地址-->  
  12.         <endpoint address="http://localhost:8999/Service1" contract="WcfService.IService1" binding="basicHttpBinding" />  
  13.       </service>  
  14.     </services>  
  15.     <behaviors>  
  16.       <serviceBehaviors>  
  17.         <behavior name="basicBehavior">  
  18.           <serviceMetadata httpGetEnabled="true" />  
  19.         </behavior>  
  20.       </serviceBehaviors>  
  21.     </behaviors>  
  22.   </system.serviceModel>  
  23.   
  24. </configuration>  
增加安装服务类。
在服务类的设计面板上,点鼠标右键,然后在弹出的菜单上,点add installer项,然后一个叫ProjectInstaller类增加成功。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。

3. 安装或卸载Windows 服务
在windows service上生成解决方案,得到exe
管理员身份运行vs2010的命令行,在exe所在目录执行installutil xxxx.exe
在服务管理中启动baowg服务
4. 客户端调用WCF服务
把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>
智慧旅游解决方案利用云计算、物联网和移动互联网技术,通过便携终端设备,实现对旅游资源、经济、活动和旅游者信息的智能感知和发布。这种技术的应用旨在提升游客在旅游各个环节的体验,使他们能够轻松获取信息、规划行程、预订票务和安排食宿。智慧旅游平台为旅游管理部门、企业和游客提供服务,包括政策发布、行政管理、景区安全、游客流量统计分析、投诉反馈等。此外,平台还提供广告促销、库存信息、景点介绍、电子门票、社交互动等功能。 智慧旅游的建设规划得到了国家政策的支持,如《国家长期科技发展规划纲要》和国务院的《关于加快发展旅游业的意见》,这些政策强调了旅游信息服务平台的建设和信息化服务的重要性。随着技术的成熟和政策环境的优化,智慧旅游的时机已经到来。 智慧旅游平台采用SaaS、PaaS和IaaS等云服务模式,提供简化的软件开发、测试和部署环境,实现资源的按需配置和快速部署。这些服务模式支持旅游企业、消费者和管理部门开发高性能、高可扩展的应用服务。平台还整合了旅游信息资源,提供了丰富的旅游产品创意平台和统一的旅游综合信息库。 智慧旅游融合应用面向游客和景区景点主管机构,提供无线城市门户、智能导游、智能门票及优惠券、景区综合安防、车辆及停车场管理等服务。这些应用通过物联网和云计算技术,实现了旅游服务的智能化、个性化和协同化,提高了旅游服务的自由度和信息共享的动态性。 智慧旅游的发展标志着旅游信息化建设的智能化和应用多样化趋势,多种技术和应用交叉渗透至旅游行业的各个方面,预示着全面的智慧旅游时代已经到来。智慧旅游不仅提升了游客的旅游体验,也为旅游管理和服务提供了高效的技术支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值