1. 创建WCF服务

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


  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实现类如下:
  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时就不会报错了。

  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文件
 
  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>

宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)的更多相关文章

  1. NetCore Selfhost,IIShost,Windows Service Host详解(自宿主、宿主在IIS,宿主在Windows Service中)

    第一部分.自托管 一.依赖.Net Core环境 修改 project.json 文件内容,增加发布时需要包含文件的配置内容(NetCore2.0版本不需要任何设置,NetCore2.0开始彻底放弃p ...

  2. 如何托管ASP.NET Core应用到Windows Service中

    (此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...

  3. ASP.NET Core应用到Windows Service中

    托管到Windows Service中 众所周知,ASP.NET Core采用了和传统ASP.NET不同的托管和HTTP处理方式,即把服务器和托管环境完全解耦. ASP.NET Core内置了两个HT ...

  4. .NET 6学习笔记(3)——在Windows Service中托管ASP.NET Core并指定端口

    在上一篇<.NET 6学习笔记(2)--通过Worker Service创建Windows Service>中,我们讨论了.NET Core 3.1或更新版本如何创建Windows Ser ...

  5. Windows Service中使用Threading.Timer需注意回收

    在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...

  6. C/C++中动态链接库的创建和调用

    DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的 ...

  7. [转贴] C/C++中动态链接库的创建和调用

    DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的 ...

  8. windows 10中的ubuntu子系统安装桌面环境的方法

    windows 10中的ubuntu子系统安装桌面环境的方法 (How to install Ubuntu-desktop in windows 10 Subsystem for Linux) 转载 ...

  9. 创建寄宿在Windows服务中的WCF服务

    1.创建Windows服务项目 2.Server1改名为你想要的名称,比如WinServer 3.在项目中新建一个WCF文件夹,用于存放wcf服务文件. 注:在WcfServer类的上面还要添加 [S ...

随机推荐

  1. JWT操作(.net)

    1.JWT定义 JWT(Json Web Token)是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准( RFC 7519 ),定义了一种简洁的,自包含的方 ...

  2. php json_encode在CI框架中的使用细节

    这个错误的造成原因是加载类类库,转换成json格式的时候不熟悉CI框架的规定导致的,CI框架中规定在将数据转换成json格式的时候需要将类库小写,当然了,调用的时候必须保证有这个类库,且可以在对应的文 ...

  3. Sql动态查询拼接字符串的优化

    Sql动态查询拼接字符串的优化 最原始的 直接写:string sql="select * from TestTables where 1=1";... 这样的代码效率很低的,这样 ...

  4. Java 支付宝支付,退款,单笔转账到支付宝账户(单笔转账到支付宝账户)

    上次分享了支付宝订单退款的代码,今天分享一下支付宝转账的操作.  现在是有一个余额提现的功能,本来是打算做提现到银行卡的,但是客户嫌麻烦不想注册银联的开放平台账户,就说先提现到支付宝就行,二期再做银行 ...

  5. java设计模式-----20、模板方法模式

    概念: Template Method模式也叫模板方法模式,是行为模式之一,它把具有特定步骤算法中的某些必要的处理委让给抽象方法,通过子类继承对抽象方法的不同实现改变整个算法的行为. 模板方法模式的应 ...

  6. Linux常用基本命令(head)

    head命令 作用:显示文件的头部内容,默认显示前面10行 格式: head [option] [file] -n <行数> -c <字节> ghostwu@dev:~/lin ...

  7. HDU3062(2-SAT)

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. C#版Aliyun DNS API

    阿里云解析API,是为域名开发者.注册商.域名代理商等提供的开放和便捷的解析服务接口.API依托于万网云解析服务,可以方便的管理域名和解析记录,让你的解析管理变的随心省时自由舒畅. 一.先附上Aliy ...

  9. 配置ArcGIS Server使用LDAP身份认证

    1.登陆ArcGIS Server Manager,修改站点的安全设置.选择用户和角色来自现有企业系统(LDAP或Windows域). 2.选择LDAP存储类型. 3.填写LDAP用户存储连接信息.主 ...

  10. Nginx的location剖析

    1.location的作用: location指令的作用是根据用户的请求的URL来执行不同的应用 2.location的语法: location [ = | ~ | ~* | ^~ ] uri { . ...