最近客户要求把服务器端程序里的二个功能用service的方式提供出来,方便调用。首先想着单独建一个wcf 服务的项目,但是因为要用到server端程序winform里的变量,因此只能在winform里添加一个wcf service的item。下面介绍详细的操作步骤:

 

1. winform里添加wcf service的item

添加之后,app.config里会自动加上wcf的配置项:

<system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name="">

          <serviceMetadata httpGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="true" />

          

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <services>

      <service name="OrayTalk.Server.Broadcast.BroadcastService">

        <endpoint address="" binding="basicHttpBinding" contract="OrayTalk.Server.Broadcast.IBroadcastService">

          <identity>

            <dns value="localhost" />

          </identity>

        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

        <host>

          <baseAddresses>

            <add baseAddress="http://localhost:8111/BroadcastService" />

          </baseAddresses>

        </host>

      </service>

    </services>

  </system.serviceModel>

 

其中下面这行,我把它改得简单了一点,默认的地址太长:

<add baseAddress="http://localhost:8111/BroadcastService" />

 

2.  定义好service,这里我用构造函数把winform里的变量传进来:

public class BroadcastService : IBroadcastService

{

    public static IRapidServerEngine m_RapidServerEngine;

    public static IOrayCache m_OrayCache;

 

    public BroadcastService(IRapidServerEngine rapidServerEngine, IOrayCache orayCache)

    {

        m_RapidServerEngine = rapidServerEngine;

        m_OrayCache = orayCache;

    }

 

    ...

}

 

3. 在form的load事件里host wcf服务, closed事件里关闭:

ServiceHost m_Host;

private void MainServerForm_Load(object sender, EventArgs e)

{

    try

    {

        BroadcastService broadcastSvc = new BroadcastService(this.rapidServerEngine, orayCache);

        m_Host = new ServiceHost(broadcastSvc);

        m_Host.Open();

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message);

    }

}

 

private void MainServerForm_FormClosed(object sender, FormClosedEventArgs e)

{

    m_Host.Close();

}

 

这里要注意的是下面二行,把变量传递进去:

BroadcastService broadcastSvc = new BroadcastService(this.rapidServerEngine, orayCache);

m_Host = new ServiceHost(broadcastSvc);

如果不用传变量到wcf服务里,可以简化成

m_Host = new ServiceHost(typeof(BroadcastService))

4. wcf服务类上加上属性

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

public class BroadcastService : IBroadcastService

5. 启动winform程序后,就可以访问在第一步里定义的wcf地址了

<add baseAddress="http://localhost:8111/BroadcastService" />

 

这时访问http://localhost:8111/BroadcastService即可。

 

 

疯吻IT

winform里宿主WCF,并传递winform变量给WCF的更多相关文章

  1. winform里操作打开在panel里的form窗体,子窗体操作同级子窗体或者父窗体的方法

    最近开始了一个winform项目,原先一直都是web项目.遇到个问题,就是在框架内,左侧和中间的main都是用panel来实现的form,就是把form窗体打开到panel里,实现左侧是导航,中间是操 ...

  2. winform里dataGridView分页代码,access数据库

    winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页. 现在c/s的程序很多时候也需要webfo ...

  3. RDLC报表 在WinForm里运行出现 未能加载文件或程序集 Microsoft.ReportViewer.WinForms, Version=11.0.0.0 System.IO.FileNotFoundException

    原文:RDLC报表 在WinForm里运行出现 未能加载文件或程序集microsoft.reportviewer.winforms 推荐以下方案二 做一下记录顺便帮助一下遇到问题的朋友. 做RDLC报 ...

  4. 在WinForm里嵌入WPF模拟公交运行状态

    公司有个公交项目,模拟公交运行的时候使用的纯WinForm技术,5秒钟刷新一次,不仅看起来感觉很丑,而且性能上很有问题,听说一段时间后就会因为内存问题崩溃(估计是没释放非托管资源吧,不断重绘,非托管资 ...

  5. 如何查看Docker容器环境变量,如何向容器传递环境变量

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 了解Docker容器的运行环境非常重要,我们把应用放在容器里执行,环境变量会直接影响程序的执行效果.所以我们要知道容器内部的 ...

  6. 【持续集成】[Jenkins]Job中如何传递自定义变量

    [Jenkins]Job中如何传递自定义变量 来自dweiwei   2015-06-27 18:37:19|  分类: 自动化测试 |举报 |字号大中小 订阅 用微信  “扫一扫” 将文章分享到朋友 ...

  7. 如何在存储过程的IN操作中传递字符串变量

    原始SQL如下: SELECT MONTH(OrderTime) AS datetype, SUM(DeliveryCount) AS decount, Region FROM (SELECT dbo ...

  8. ssh时传递环境变量

    设置要传递的变量: -o SendEnv=Varname 但是不是每个都能传,受服务器上sshd_config里的下面两个选项的控制: AcceptEnv and PermitUserEnvironm ...

  9. 【问题】Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数

    [问题]Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数. [解决]直接对变量加引号,如: <button onclick="deleteProduct('@ ...

随机推荐

  1. Spring 整合Mybatis实例

    演示样例下载地址:http://download.csdn.net/detail/geloin/4506640 本文基于Spring 注解,让Spring跑起来.本文使用Mysql数据库. (1) 导 ...

  2. RTSP server 在mips 上莫名其妙退出(PC上则无此问题)

    http://blog.csdn.net/lubing20044793/article/details/38523701 早在这篇blog以前写过,在虚拟机下调试sn9c291时,USB 数据传输出了 ...

  3. Python3入门(一)——概述与环境安装

    一.概述 1.python是什么 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和 ...

  4. Vue 项目集合

    饿了么安全应急响应中心 饿了么招聘 饿了么前端 · GitHub 稀土掘金 异乡好居 明星垂搜 广州建管 基于Vue.js的数据统计系统(一) 基于Vue.js的数据统计系统(二) 基于Vue.js的 ...

  5. Linux中tty、pty、pts的概念区别 转载

    基本概念: > tty(终端设备的统称): tty一词源于Teletypes,或teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西 ...

  6. Redis学习之路(三)之Redis主从和哨兵模式

    目录 一.Redis主从配置 1.环境说明 2.修改主从的redis配置文件 3.启动主从redis 3.数据同步验证 二.Redis哨兵模式 1.Redis sentinel介绍 2.Redis S ...

  7. 微信小程序在当前页面设置其他页面的数据

    如果其他页面用到的数据是 globalData, 那么直接在当前页面修改 globalData 数据即可. 如果其他页面用到的数据是 storage, 那么直接在当前页面修改 storage 数据即可 ...

  8. HyperLedger/Fabric JAVA-SDK with 1.1

    HyperLedger/Fabric JAVA-SDK with 1.1 该项目可直接在github上访问. 该项目介绍如何使用fabric-sdk-java框架,基于fabric-sdk-java ...

  9. 微软职位内部推荐-Senior Software Engineer - Front End

    微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...

  10. PHP学习 流程控制和数组

    flow control 流程控制decision structure 判断结构loop structure 循环结构 if(condition){statement1;} if(){}else{} ...