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的控制 ...
随机推荐
- 吴裕雄 python深度学习与实践(10)
import tensorflow as tf input1 = tf.constant(1) print(input1) input2 = tf.Variable(2,tf.int32) print ...
- idea常用快捷键及操作
ctrl+j ===== 智能提示 可用模版及关键字 ctrl+p ===== 显示方法可填入的参数 ctrl+space ===== 补全提示项目中可用的变量 ctrl+shift+j ==== ...
- 清理xcode
移除 Xcode 运行安装 APP 产生的缓存文件(DerivedData) ~/Library/Developer/Xcode/DerivedData 移除 APP 打包的ipa历史版本(Archi ...
- Linux终端命令
一.文件目录类 1.建立目录:mkdir 目录名2.删除空目录:rmdir 目录名3.无条件删除子目录: rm -rf 目录名4.改变当前目录:cd 目录名 (进入用户home目录:cd ~;进入上一 ...
- SpringAOP日志配置
SpringAOP日志配置 配置文件配置 l 配置spring-mvc.xml <aop:config proxy-target-class="true" /> &l ...
- 49-Python 安装pythoncom库和pyHook
这个直接用pip不行,所以借鉴了别人的方法: YTouchCoder 1. https://sourceforge.net/projects/pywin32/files/pywin32/ 这里面下载p ...
- win10安装MongoDB提示 the domain,user name and/or password are incorrect. Remember to use "." for the domain if the account is on the local machine.
好心塞,提示输入不合法. 后来发现这样可以解决.退出安装.重新打开()因为我第一次打开时是没有卡在这一步的,只不过返回上一页时就一直卡在验证的页面了),默认,默认,默认,
- xib中的label加边框
选中xib中的label,在右边栏的第三个标签页中第三项是User Defined Runtime Attributes 添加一个keyPath,keyPath值为layer.borderWidth, ...
- 查看class实际执行的类路径
查看class真实归属的jar包位置getClass().getClassLoader().getResource(getClass().getName().replace('.', '/') + & ...
- wincvs配置方法
1.安装wincvs_中文1.3.exe 2.安装cvsnt-2.5.03.2260.msi 安装过程中选择complete选项 3.安装python221-setup.exe 安装完成后,先配置环 ...