契约

新建一个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寄宿到控制台应用程序的更多相关文章

  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. linux目录与文件权限的意义

    现在我们已经知道了Linux系统内文件的三种身份(所有者,用户者,与其他人),知道每种身份都有三种属性(r,w,x),已经能够使用chown,chgrp,chmod去修改这些权限和属性,那么这些文件权 ...

  2. 【原】The Linux Command Line - Workiing with commands

    ● type – Indicate how a command name is interpreted● which – Display which executable program will b ...

  3. ArcGIS自定义工具箱-列举损坏的数据源

    ArcGIS自定义工具箱-列举损坏的数据源 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:查找地图文档中损坏的数据源链接 使用方法:参数可选,默认为当前(ar ...

  4. Could not find a package,configuration file provided by "G2O" ,G2OConfig.cmake,g2o-config.cmake

    因为项目需要使用到g2o,所以自己从git上面clone下来, git clone https://github.com/RainerKuemmerle/g2o.git 然后: cd g2o mkdi ...

  5. position:fix相对父元素定位

    大家都知道,当position的值为fix时,生成绝对定位的元素,相对于浏览器窗口进行定位. 它常常应用的场合是,当下拉滚动条时固定导航栏到顶部,将广告固定在页面两侧或浏览器中间. 如果需要将导航栏d ...

  6. Linux redhat 7 进入单用户模式

    redhat  7 进入单用户模式修复系统故障 1.启动机器,grub界面选择第一个,按e 2.往下翻,找到Linux16 开头的那一行 3.将ro改为"rw init=/sysroot/b ...

  7. 【转】Appium 优化版

    Appium 开源分享优化版 之前分享过PageObject+Python+Appium 本版本是对上次版本较大改版,主要解决了: 失败重连一次(默认一次)可配置多次 基于appium1.7.1 ui ...

  8. 天天向上的力量 III

    描述 一年365天,以第1天的能力值为基数,记为1.0. 当好好学习时,能力值相比前一天提高N‰:当没有学习时,能力值相比前一天下降N‰. 每天努力或放任,一年下来的能力值相差多少呢?其中,N的取值范 ...

  9. pythone函数基础(8)内置函数学习

    内置函数学习# sorted# map# filter# max# sum# round# chr# ord# dir# bool# eval# exec# zipimport mathres = m ...

  10. node.js中通过dgram数据报模块创建UDP服务器和客户端

    node.js中 dgram 模块提供了udp数据包的socket实现,可以方便的创建udp服务器和客户端. 一.创建UDP服务器和客户端 服务端: const dgram = require('dg ...