WCF绑定netTcpBinding寄宿到控制台应用程序
契约
新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService
代码如下:
- [ServiceContract]
- public interface IGameService
- {
- [OperationContract]
- Task<string> DoWork(string arg);
- }
- public class GameService : IGameService
- {
- public async Task<string> DoWork(string arg)
- {
- return await Task.FromResult($"Hello {arg}, I am the GameService.");
- }
- }
- [ServiceContract]
- public interface IPlayerService
- {
- [OperationContract]
- Task<string> DoWork(string arg);
- }
- public class PlayerService : IPlayerService
- {
- public async Task<string> DoWork(string arg)
- {
- return await Task.FromResult($"Hello {arg}, I am the PlayerService.");
- }
- }
服务端
新建一个控制台应用程序,添加一个类 ServiceHostManager
- public interface IServiceHostManager : IDisposable
- {
- void Start();
- void Stop();
- }
- public class ServiceHostManager<TService> : IServiceHostManager
- where TService : class
- {
- ServiceHost _host;
- public ServiceHostManager()
- {
- _host = new ServiceHost(typeof(TService));
- _host.Opened += (s, a) => {
- Console.WriteLine("WCF监听已启动!{0}", _host.Description.Endpoints[].Address);
- };
- _host.Closed += (s, a) =>
- {
- Console.WriteLine("WCF服务已终止!{0}", _host.Description.Endpoints[].Name);
- };
- }
- public void Start()
- {
- Console.WriteLine("正在开启WCF服务...{0}", _host.Description.Endpoints[].Name);
- _host.Open();
- }
- public void Stop()
- {
- if (_host != null && _host.State == CommunicationState.Opened)
- {
- Console.WriteLine("正在关闭WCF服务...{0}", _host.Description.Endpoints[].Name);
- _host.Close();
- }
- }
- public void Dispose()
- {
- Stop();
- }
- public static Task StartNew(CancellationTokenSource cancelTokenSource)
- {
- var theTask = Task.Factory.StartNew(() =>
- {
- IServiceHostManager shs = null;
- try
- {
- shs = new ServiceHostManager<TService>();
- shs.Start();
- while (true)
- {
- if (cancelTokenSource.IsCancellationRequested && shs != null)
- {
- shs.Stop();
- break;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- if (shs != null)
- shs.Stop();
- }
- }, cancelTokenSource.Token);
- return theTask;
- }
- }
在Main方法中启动WCF主机
- class Program
- {
- static Program()
- {
- Console.WriteLine("初始化...");
- Console.WriteLine("服务运行期间,请不要关闭窗口。");
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- Console.Title = "WCF主机 x64.(按 [Esc] 键停止服务)";
- var cancelTokenSource = new CancellationTokenSource();
- ServiceHostManager<WcfContract.Services.GameService>.StartNew(cancelTokenSource);
- ServiceHostManager<WcfContract.Services.PlayerService>.StartNew(cancelTokenSource);
- while (true)
- {
- if (Console.ReadKey().Key == ConsoleKey.Escape)
- {
- Console.WriteLine();
- cancelTokenSource.Cancel();
- break;
- }
- }
- Console.ReadLine();
- }
- }
服务端配置
在控制台应用程序的App.config中配置system.serviceModel
- <system.serviceModel>
- <services>
- <service name="Wettery.WcfContract.Services.GameService" behaviorConfiguration="gameMetadataBehavior">
- <endpoint address="net.tcp://localhost:19998/Wettery/GameService" binding="netTcpBinding" contract="Wettery.WcfContract.Services.IGameService" bindingConfiguration="netTcpBindingConfig">
- <identity>
- <dns value="localhost" />
- </identity>
- </endpoint>
- </service>
- <service name="Wettery.WcfContract.Services.PlayerService" behaviorConfiguration="playerMetadataBehavior">
- <endpoint address="net.tcp://localhost:19998/Wettery/PlayerService" binding="netTcpBinding" contract="Wettery.WcfContract.Services.IPlayerService" bindingConfiguration="netTcpBindingConfig">
- <identity>
- <dns value="localhost" />
- </identity>
- </endpoint>
- </service>
- </services>
- <bindings>
- <netTcpBinding>
- <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">
- <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647 " maxBytesPerRead="4096" maxNameTableCharCount="16384" />
- <reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="false" />
- <security mode="Transport">
- <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
- </security>
- </binding>
- </netTcpBinding>
- </bindings>
- <behaviors>
- <serviceBehaviors>
- <behavior name="gameMetadataBehavior">
- <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8081/Wettery/GameService/MetaData" />
- <serviceDebug includeExceptionDetailInFaults="True" />
- <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" />
- </behavior>
- <behavior name="playerMetadataBehavior">
- <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8081/Wettery/PlayerService/MetaData" />
- <serviceDebug includeExceptionDetailInFaults="True" />
- <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
未避免元数据泄露,部署时将HttpGetEnable设为False
运行控制台应用程序
按[ESC]键终止服务
客户端测试
服务端运行后,用wcftestclient工具测试,服务地址即behavior中配置的元数据GET地址
http://localhost:8081/Wettery/GameService/MetaData
http://localhost:8081/Wettery/PlayerService/MetaData
WCF绑定netTcpBinding寄宿到控制台应用程序的更多相关文章
- WCF绑定netTcpBinding寄宿到IIS
继续沿用上一篇随笔中WCF服务类库 Wettery.WcfContract.Services WCF绑定netTcpBinding寄宿到控制台应用程序 服务端 添加WCF服务应用程序 Wettery. ...
- 创建WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...
- WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...
- 通过代码的方式完成WCF服务的寄宿工作
使用纯代码的方式进行服务寄宿 服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境.通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用, ...
- 20181101_将WCF寄宿到控制台
使用管理员权限打开VS2017 2. 创建以下代码进行测试: a) 创建一个空白解决方案 b) 创建三个类库文件 c) IMathService代码如下 ...
- 添加宿主为控制台应用程序的WCF服务
1.创建WCF服务库:WcfServiceLibrary,根据自动创建的代码修改自己的WCF 服务协议.操作协议.数据协议.本次先实现简单的WCF最基本的通信方式:请求->应答模式. 定义服务. ...
- WCF绑定类型选择
WCF绑定类型选择 发布日期:2010年12月10日星期五 作者:EricHu 在开发WCF程序时,如何选择一个适合的绑定对于消息传输的可靠性,传输模式是否跨进程.主机.网络,传输模式的支持. ...
- 基于UDP协议的控制台聊天程序(c++版)
本博客由Rcchio原创,转载请告知作者 ------------------------------------------------------------------------------- ...
- ArcEngine控制台应用程序
转自wbaolong原文 ArcEngine控制台应用程序 控制台应用程序相比其他应用程序,更加简单,简化了许多冗余,可以让我们更加关注于本质的东西. 现在让我们看一看ArcGIS Engine的控制 ...
随机推荐
- CentOS6.5搭建OpenVas完全搭建手册(搭建过程总结及小记)
一.OpenVAS 介绍 1.关于OpenVAS OpenVAS(Open Vulnerability Assessment System)是一套开源的漏洞扫描系统,早期Nessus 是其中一个最流行 ...
- 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 ...
- 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环 ...
- ABAP级别【技能树】
- kettle学习笔记(二)——kettle基本使用
一.子程序功能和启动方式介绍 Spoon.bat: 图形界面方式启动作业和转换设计器. Pan.bat: 命令行方式执行转换. Kitchen.bat: 命令行方式执行作业. Carte.bat: 启 ...
- Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题(转)
原文地址:https://wzfou.com/php-fpm/ 最近发现博客的内存老是隔三差五地被“吃掉”了,登录到后台后偶尔会出卡顿的情况,一开始怀疑是Swap不够导致的,于是给VPS主机增加了几个 ...
- linux resin 基本站点配置
进入配置文件目录: [root@linuxidc resin-]# cd /usr/local/resin/conf/ 查看都有哪些配置文件: [root@linuxidc conf]# ls app ...
- Java代码获取spring 容器的bean几种方式
一.目的 写了一个项目,多个module,然后想在A模块中实现固定的config注入,当B模块引用A时候,能够直接填写相对应的配置信息就行了.但是遇到一个问题,B引用A时候,A的配置信息总是填充不了, ...
- C++ static 静态变量&静态成员函数
.h文件中定义 static变量后,如 static QTcpSocket * socket; 那么一定要在.cpp中 构造函数的外面将其初始化为 QTcpSocket * Cfiletransfer ...
- SLICK基础
1.sbt添加依赖 "com.typesafe.slick" %% "slick" % "3.2.3", "org.slf4j&q ...