契约

新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService

代码如下:

  1. [ServiceContract]
  2. public interface IGameService
  3. {
  4. [OperationContract]
  5. Task<string> DoWork(string arg);
  6. }
  1. public class GameService : IGameService
  2. {
  3. public async Task<string> DoWork(string arg)
  4. {
  5. return await Task.FromResult($"Hello {arg}, I am the GameService.");
  6. }
  7. }
  1. [ServiceContract]
  2. public interface IPlayerService
  3. {
  4. [OperationContract]
  5. Task<string> DoWork(string arg);
  6. }
  1. public class PlayerService : IPlayerService
  2. {
  3. public async Task<string> DoWork(string arg)
  4. {
  5. return await Task.FromResult($"Hello {arg}, I am the PlayerService.");
  6. }
  7. }

服务端

新建一个控制台应用程序,添加一个类 ServiceHostManager

  1. public interface IServiceHostManager : IDisposable
  2. {
  3. void Start();
  4. void Stop();
  5. }
  6.  
  7. public class ServiceHostManager<TService> : IServiceHostManager
  8. where TService : class
  9. {
  10. ServiceHost _host;
  11.  
  12. public ServiceHostManager()
  13. {
  14. _host = new ServiceHost(typeof(TService));
  15. _host.Opened += (s, a) => {
  16. Console.WriteLine("WCF监听已启动!{0}", _host.Description.Endpoints[].Address);
  17. };
  18. _host.Closed += (s, a) =>
  19. {
  20. Console.WriteLine("WCF服务已终止!{0}", _host.Description.Endpoints[].Name);
  21. };
  22. }
  23. public void Start()
  24. {
  25. Console.WriteLine("正在开启WCF服务...{0}", _host.Description.Endpoints[].Name);
  26. _host.Open();
  27. }
  28. public void Stop()
  29. {
  30. if (_host != null && _host.State == CommunicationState.Opened)
  31. {
  32. Console.WriteLine("正在关闭WCF服务...{0}", _host.Description.Endpoints[].Name);
  33. _host.Close();
  34. }
  35. }
  36. public void Dispose()
  37. {
  38. Stop();
  39. }
  40.  
  41. public static Task StartNew(CancellationTokenSource cancelTokenSource)
  42. {
  43. var theTask = Task.Factory.StartNew(() =>
  44. {
  45. IServiceHostManager shs = null;
  46. try
  47. {
  48. shs = new ServiceHostManager<TService>();
  49. shs.Start();
  50. while (true)
  51. {
  52. if (cancelTokenSource.IsCancellationRequested && shs != null)
  53. {
  54. shs.Stop();
  55. break;
  56. }
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. Console.WriteLine(ex);
  62. if (shs != null)
  63. shs.Stop();
  64. }
  65. }, cancelTokenSource.Token);
  66.  
  67. return theTask;
  68. }
  69. }

在Main方法中启动WCF主机

  1. class Program
  2. {
  3. static Program()
  4. {
  5. Console.WriteLine("初始化...");
  6. Console.WriteLine("服务运行期间,请不要关闭窗口。");
  7. Console.WriteLine();
  8. }
  9.  
  10. static void Main(string[] args)
  11. {
  12. Console.Title = "WCF主机 x64.(按 [Esc] 键停止服务)";
  13. var cancelTokenSource = new CancellationTokenSource();
  14. ServiceHostManager<WcfContract.Services.GameService>.StartNew(cancelTokenSource);
  15. ServiceHostManager<WcfContract.Services.PlayerService>.StartNew(cancelTokenSource);
  16. while (true)
  17. {
  18. if (Console.ReadKey().Key == ConsoleKey.Escape)
  19. {
  20. Console.WriteLine();
  21. cancelTokenSource.Cancel();
  22. break;
  23. }
  24. }
  25. Console.ReadLine();
  26. }
  27. }

服务端配置

在控制台应用程序的App.config中配置system.serviceModel

  1. <system.serviceModel>
  2. <services>
  3. <service name="Wettery.WcfContract.Services.GameService" behaviorConfiguration="gameMetadataBehavior">
  4. <endpoint address="net.tcp://localhost:19998/Wettery/GameService" binding="netTcpBinding" contract="Wettery.WcfContract.Services.IGameService" bindingConfiguration="netTcpBindingConfig">
  5. <identity>
  6. <dns value="localhost" />
  7. </identity>
  8. </endpoint>
  9. </service>
  10. <service name="Wettery.WcfContract.Services.PlayerService" behaviorConfiguration="playerMetadataBehavior">
  11. <endpoint address="net.tcp://localhost:19998/Wettery/PlayerService" binding="netTcpBinding" contract="Wettery.WcfContract.Services.IPlayerService" bindingConfiguration="netTcpBindingConfig">
  12. <identity>
  13. <dns value="localhost" />
  14. </identity>
  15. </endpoint>
  16. </service>
  17. </services>
  18. <bindings>
  19. <netTcpBinding>
  20. <binding name="netTcpBindingConfig" closeTimeout="00:30:00" openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="100" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="100" maxReceivedMessageSize="2147483647">
  21. <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647 " maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  22. <reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="false" />
  23. <security mode="Transport">
  24. <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
  25. </security>
  26. </binding>
  27. </netTcpBinding>
  28. </bindings>
  29. <behaviors>
  30. <serviceBehaviors>
  31. <behavior name="gameMetadataBehavior">
  32. <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8081/Wettery/GameService/MetaData" />
  33. <serviceDebug includeExceptionDetailInFaults="True" />
  34. <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" />
  35. </behavior>
  36. <behavior name="playerMetadataBehavior">
  37. <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8081/Wettery/PlayerService/MetaData" />
  38. <serviceDebug includeExceptionDetailInFaults="True" />
  39. <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" />
  40. </behavior>
  41. </serviceBehaviors>
  42. </behaviors>
  43. </system.serviceModel>

未避免元数据泄露,部署时将HttpGetEnable设为False

运行控制台应用程序

按[ESC]键终止服务

客户端测试

服务端运行后,用wcftestclient工具测试,服务地址即behavior中配置的元数据GET地址

http://localhost:8081/Wettery/GameService/MetaData
http://localhost:8081/Wettery/PlayerService/MetaData

WCF绑定netTcpBinding寄宿到控制台应用程序的更多相关文章

  1. WCF绑定netTcpBinding寄宿到IIS

    继续沿用上一篇随笔中WCF服务类库 Wettery.WcfContract.Services WCF绑定netTcpBinding寄宿到控制台应用程序 服务端 添加WCF服务应用程序 Wettery. ...

  2. 创建WCF服务自我寄宿

    WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...

  3. WCF服务自我寄宿

    WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...

  4. 通过代码的方式完成WCF服务的寄宿工作

    使用纯代码的方式进行服务寄宿 服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境.通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用, ...

  5. 20181101_将WCF寄宿到控制台

    使用管理员权限打开VS2017 2. 创建以下代码进行测试: a)         创建一个空白解决方案 b)         创建三个类库文件 c)         IMathService代码如下 ...

  6. 添加宿主为控制台应用程序的WCF服务

    1.创建WCF服务库:WcfServiceLibrary,根据自动创建的代码修改自己的WCF 服务协议.操作协议.数据协议.本次先实现简单的WCF最基本的通信方式:请求->应答模式. 定义服务. ...

  7. WCF绑定类型选择

    WCF绑定类型选择   发布日期:2010年12月10日星期五 作者:EricHu   在开发WCF程序时,如何选择一个适合的绑定对于消息传输的可靠性,传输模式是否跨进程.主机.网络,传输模式的支持. ...

  8. 基于UDP协议的控制台聊天程序(c++版)

    本博客由Rcchio原创,转载请告知作者 ------------------------------------------------------------------------------- ...

  9. ArcEngine控制台应用程序

    转自wbaolong原文 ArcEngine控制台应用程序 控制台应用程序相比其他应用程序,更加简单,简化了许多冗余,可以让我们更加关注于本质的东西. 现在让我们看一看ArcGIS Engine的控制 ...

随机推荐

  1. CentOS6.5搭建OpenVas完全搭建手册(搭建过程总结及小记)

    一.OpenVAS 介绍 1.关于OpenVAS OpenVAS(Open Vulnerability Assessment System)是一套开源的漏洞扫描系统,早期Nessus 是其中一个最流行 ...

  2. LeetCode OJ 129. Sum Root to Leaf Numbers

    题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...

  3. tensorflow/pytorch/mxnet的pip安装,非源代码编译,基于cuda10/cudnn7.4.1/ubuntu18.04.md

    os安装 目前对tensorflow和cuda支持最好的是ubuntu的18.04 ,16.04这种lts,推荐使用18.04版本.非lts的版本一般不推荐. Windows倒是也能用来装深度GPU环 ...

  4. ABAP级别【技能树】

  5. kettle学习笔记(二)——kettle基本使用

    一.子程序功能和启动方式介绍 Spoon.bat: 图形界面方式启动作业和转换设计器. Pan.bat: 命令行方式执行转换. Kitchen.bat: 命令行方式执行作业. Carte.bat: 启 ...

  6. Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题(转)

    原文地址:https://wzfou.com/php-fpm/ 最近发现博客的内存老是隔三差五地被“吃掉”了,登录到后台后偶尔会出卡顿的情况,一开始怀疑是Swap不够导致的,于是给VPS主机增加了几个 ...

  7. linux resin 基本站点配置

    进入配置文件目录: [root@linuxidc resin-]# cd /usr/local/resin/conf/ 查看都有哪些配置文件: [root@linuxidc conf]# ls app ...

  8. Java代码获取spring 容器的bean几种方式

    一.目的 写了一个项目,多个module,然后想在A模块中实现固定的config注入,当B模块引用A时候,能够直接填写相对应的配置信息就行了.但是遇到一个问题,B引用A时候,A的配置信息总是填充不了, ...

  9. C++ static 静态变量&静态成员函数

    .h文件中定义 static变量后,如 static QTcpSocket * socket; 那么一定要在.cpp中 构造函数的外面将其初始化为 QTcpSocket * Cfiletransfer ...

  10. SLICK基础

    1.sbt添加依赖 "com.typesafe.slick" %% "slick" % "3.2.3", "org.slf4j&q ...