Search code examples
c#wpfwcfservicehost

WCF channel factory timeout in WPF Application


I am hosting a WCF service in a WPF application in C#. 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.

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?


Solution

  • 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();