简体   繁体   English

WPF应用程序中的WCF通道工厂超时

[英]WCF channel factory timeout in WPF Application

I am hosting a WCF service in a WPF application in C#. 我在C#的WPF应用程序中托管WCF服务。 I would like to call some initializing functions from the host itself so I am using a channel factory. 我想从主机本身调用一些初始化函数,所以我正在使用通道工厂。 When the host is open I can call the WCF service methods from another application but when I run the following code in the host I get a time out on the Test() method. 当主机打开时,我可以从另一个应用程序调用WCF服务方法,但是当我在主机中运行以下代码时,Test()方法会超时。

host = new ServiceHost(typeof(SequencerService), new Uri(address));
host.Open();

var binding = new NetTcpBinding(SecurityMode.None);
EndpointAddress endpoint = new EndpointAddress("net.tcp://10.0.0.118:50111/SequencerService");
ChannelFactory<ISequencerService> myFactory = new ChannelFactory<ISequencerService>(binding, endpoint);
ISequencerService mService = myFactory.CreateChannel();

Console.WriteLine(mService.Test());

I get locked on the last line of code. 我被锁定在代码的最后一行。 Also, if I try to host this code in a console application it works fine. 另外,如果我尝试将此代码托管在控制台应用程序中,则效果很好。 Why is there a difference when running this in a WPF windows application? 为什么在WPF Windows应用程序中运行它时会有区别?

I just found the answer. 我才找到答案。 It seems like the service must be hosted in a separate thread than it is consumed in. To host I used: 看来该服务必须托管在一个单独的线程中,而不是使用该线程。要托管,我使用了:

new Thread(()=>{
    host = new ServiceHost(typeof(SequencerService), new Uri(address));
    host.Open();
}.Start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM