- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章WCF如何绑定netTcpBinding寄宿到控制台应用程序详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
契约 。
新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService 。
代码如下:
1
2
3
4
5
6
|
[ServiceContract]
public
interface
IGameService
{
[OperationContract]
Task<
string
> DoWork(
string
arg);
}
|
1
2
3
4
5
6
7
|
public
class
GameService : IGameService
{
public
async Task<
string
> DoWork(
string
arg)
{
return
await Task.FromResult($
"Hello {arg}, I am the GameService."
);
}
}
|
1
2
3
4
5
6
|
[ServiceContract]
public
interface
IPlayerService
{
[OperationContract]
Task<
string
> DoWork(
string
arg);
}
|
1
2
3
4
5
6
7
|
public
class
PlayerService : IPlayerService
{
public
async Task<
string
> DoWork(
string
arg)
{
return
await Task.FromResult($
"Hello {arg}, I am the PlayerService."
);
}
}
|
服务端 。
新建一个控制台应用程序,添加一个类 ServiceHostManager 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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[0].Address);
};
_host.Closed += (s, a) =>
{
Console.WriteLine(
"WCF服务已终止!{0}"
, _host.Description.Endpoints[0].Name);
};
}
public
void
Start()
{
Console.WriteLine(
"正在开启WCF服务...{0}"
, _host.Description.Endpoints[0].Name);
_host.Open();
}
public
void
Stop()
{
if
(_host !=
null
&& _host.State == CommunicationState.Opened)
{
Console.WriteLine(
"正在关闭WCF服务...{0}"
, _host.Description.Endpoints[0].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主机 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<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 。
运行控制台应用程序 。
客户端测试 。
服务端运行后,用wcftestclient工具测试,服务地址即behavior中配置的元数据GET地址 。
http://localhost:8081/Wettery/GameService/MetaData 。
http://localhost:8081/Wettery/PlayerService/MetaData 。
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持.
原文链接:https://www.cnblogs.com/felixnet/p/7397139.html 。
最后此篇关于WCF如何绑定netTcpBinding寄宿到控制台应用程序详解的文章就讲到这里了,如果你想了解更多关于WCF如何绑定netTcpBinding寄宿到控制台应用程序详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我需要检查在我的 RCP 应用程序中启动时是否加载了某些包。我知道有一个“主机 OSGi 控制台”可以显示 Eclipse IDE 中所有插件的状态,但我对这些不感兴趣。 我执行了以下步骤来获取我的应
在 pdb/ipdb 调试中,有用的 interact 命令为我提供了一个功能齐全的交互式 Python 控制台。 但是,这似乎始终是“标准”Python 控制台,即使我使用 ipdb 开始也是如此。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我搜索过但找不到答案:如何在运行 Emacs 时选择:文件、编辑、选项、缓冲区、工具、C++ 等下拉菜单在控制台模式下?不是终端菜单。 不,F10 不是答案。 最佳答案 如果不是 F10,那么 M-x
我正在制作一个每 20-40 秒截屏一次的 c# 控制台应用程序。 我试过到处找,但所有其他示例都没有使用控制台 这是我到目前为止所做的代码: using System; using System.D
尝试使用 terraform 控制台,新功能。 我使用 tfstate 进入我的项目并运行“terraform 控制台”。 我可以使用常规插值系统获取变量值、数据和资源。但是,模块很难破解,我无法正确
我正在尝试调试一段返回错误的 SQL。我不确定 django 或 mysql 是否处理错误,所以我想通过 django 控制台运行它。 有办法设置吗? 提前致谢。 最佳答案 manage.py dbs
你好是否可以在 JPanel 中绘制 java 控制台返回的内容?你有教程可以遵循吗?谢谢开关 最佳答案 我不记得在哪里找到这个,但我已使用我称为 TextAreaOutputStream 的类将输出
我对 Xcode 甚至编程都有点陌生。 在 Xcode 中,在我的代码中,如何显示控制台并清除屏幕? 我知道我可以使用 Xcode 首选项来完成此操作,但我想以编程方式完成此操作。 最佳答案 这对我有
我正在开发一个 C# 项目,我需要从没有 API 或 Web 服务的安全网站获取数据。我的计划是登录,访问我需要的页面,并解析 HTML 以获取记录到数据库所需的数据位。现在我正在使用控制台应用程序进
我是编程新手,正在尝试不同的在线事件以掌握它。我遇到了一个特定的问题,我想制作一个程序,用户输入一个值并打印一个特定的字符串。例如,当用户输入 0 时,将打印字符串“black”,输入 1 将打印字符
我想创建一个终端/控制台,用户可以在其中输入命令。我知道 java,但我是 xml 的新手,所以我想知道如何在文本下生成文本,如果它变得很长,它应该是可滚动的,这是一张图片: 这是我的 xml cpd
我有一个由随机生成的数字组成的 nxn 网格。我有一个标签显示 X 轴和 Y 轴的元素编号: 对于单个数字,它可以正确对齐,但是当网格大小增加时,标签会变得不成比例并且不会像这样对齐: 我想知道是否有
假设我创建了一个包含两个变量的结构。 struct mystruct{ public: string name; int age;}; class School :public mystruct{ p
我正在重写一个服务器程序,我想在其中添加一个简单的控制台输入。 目前,它只是提供数据并为它所做的每一件事打印出一两行,作为任何观看/调试的人的描述性措施。 我想要的是有一个始终位于底部的“粘性”输入栏
我必须编写启动另一个进程(GUI)的控制台应用程序。然后,使用其他应用程序或相同的选项,我必须能够停止子进程。此外,如果子进程从 GUI 关闭,则必须通知我执行最终任务(如果被杀死,则相同)。 我认为
我一直在尝试到处寻找以下问题的答案: Linux上的标准输出/控制台默认将内容保存到文件中吗? 我不想保存内容或重定向输出(我已经知道这一点),我只是想知道它是否已经通过 linux 中包含的某个默认
我正在尝试不同的事件,因为我是初学者并且想了解更多。我正在尝试在我的代码所在的同一行打印一个图案: int main() { int numOfWiggles; int count;
在我的一项小任务中,我被要求创建一个数组来存储从用户提供的输入中获取的姓名和地址,并且稍后能够从数组中删除姓名和地址。 如果能帮助我理解如何实现这一目标,我们将不胜感激,谢谢。 编辑 - 该数组将像地
如果您想在 Python shell 中查看特定模块中定义了哪些模块,一种选择是键入 dir(path.to.module)。不幸的是,这不仅列出了特定模块中定义的类或函数,还包括该模块导入的类或函数
我是一名优秀的程序员,十分优秀!