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的控制 ...
随机推荐
- LevelDB源码分析-Compact
Compaction compact由背景线程完成,代码中触发背景线程的函数为: void DBImpl::MaybeScheduleCompaction() { mutex_.AssertHeld( ...
- <Dare To Dream 团队>第二次作业:基于B/S的家教管理系统
团队项目GitHub仓库地址:https://github.com/Sophur/Team-Project.git 为其他团队评分结果: 小组名 N A B C D 总分 平均分 Blue Flk ...
- Nginx搭建
Nginx nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件. nginx因具有高并发(特别是静态资源),占用系统资源少等特性,且功能丰富而逐渐流行起来. nginx不但是一个优秀 ...
- Django-ConttentType
一 content-type 在django中,有一个记录了项目中所有model元数据的表,就是ContentType,表中一条记录对应着一个存在的model,所以可以通过一个ContentType表 ...
- echart.js在vue中使用
以前可能写过,懒得去翻了,再写一次 1,安装echarts 导入到页面 import echarts from 'echarts'; 2.在生命周期里面做初始化 data(){ return{ t ...
- centos 7 安装python 3.x
首先 安装一些 可能需要的依赖: yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-d ...
- 20172306 2018-2019 《Java程序设计与数据结构》第一周学习总结
20172306 2018-2019 <Java程序设计与数据结构(下)>第一周学习总结 教材学习内容总结 第一章 概述 (程序=数据结构+算法 软件=程序+软件工程) 1.1 软件质量 ...
- Effective C++ 笔记:条款 30 inline
30 : Understand the ins and outs of inlining 1 inline申请书 1.1 类内部实现函数包含隐藏的inline申请 class Human { publ ...
- IT资产管理—采购与合同管理功能
- 别人的Linux私房菜(6)文件权限与目录配置
账号与一般身份用户存放在/etc/passwd文件中 个人密码存放在/etc/shadow文件中 Linux所有组名存放在/etc/group中 ls -al查看所有信息并显示权限等 文件权限的10字 ...