Duplex WCF communication doesn't work in Metro application
-
28-10-2019 - |
Question
Could you please tell me why duplex communication with TcpTransportBindingElement doesn't work in my Metro application ?
According to this document Metro subset of .Net Framework supports TCP binding.
So I wrote WCF server as console application. Here is source code:
static void Main()
{
UiWcfSession.OnInitialize += ClientInitialize;
var baseAddresses = new Uri("net.tcp://localhost:9000/");
var host = new ServiceHost(typeof(UiWcfSession), baseAddresses);
var reliableSession = new ReliableSessionBindingElement { Ordered = true, InactivityTimeout = TimeSpan.MaxValue };
var binding =
new CustomBinding(reliableSession, new TcpTransportBindingElement()) { ReceiveTimeout = TimeSpan.MaxValue };
host.AddServiceEndpoint(typeof(IClientFulfillmentPipeService), binding, "");
var metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
var mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
host.Open();
Thread.CurrentThread.Join();
}
private static void ClientInitialize(int uiprocessid, string key)
{
Debug.WriteLine("ClientInitialize");
}
and here is client code in Metro app:
partial class MainPage
{
public MainPage()
{
InitializeComponent();
}
private void onclick(object sender, RoutedEventArgs e)
{
try
{
var ep = new EndpointAddress("net.tcp://localhost:9000/");
var binding = new CustomBinding(new TcpTransportBindingElement());
var ctx = new InstanceContext(new Wrapper());
var pipeFactory = new DuplexChannelFactory<IClientFulfillmentPipeService>(ctx, binding, ep);
IClientFulfillmentPipeService commChannel = pipeFactory.CreateChannel();
// open up the the comm channel with a reasonable timeout...
((IChannel)commChannel).Open();
commChannel.Initialize(1234, "Test");
((IChannel)commChannel).Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
So it kinda works. But when I walk step by step in the debugger in Metro app it hangs and never returns from the function commChannel.Initialize
.
Why is it happening ? What am I missing ?
Solution
Turns out I can't use synchronous client in Metro. I have to use asynchronous calls. That's why it didn't work.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow