WCF client freezes when calling a method from the Service

I have this strange problem when my client will freeze when it calls a method from my WCF service. Now it’s strange that this does not happen when the client is a console application. This happens when the client is a WinForm or WPF application.

I created a client library that the WCF client can use to connect to the Service, here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;  //needed for WCF communication

namespace DCC_Client
{
    public class DCCClient
    {
        private DuplexChannelFactory<ServiceReference1.IDCCService> dualFactory;

        public ServiceReference1.IDCCService Proxy;

        public DCCClient()
        {
            //Setup the duplex channel to the service...
            NetNamedPipeBinding binding = new NetNamedPipeBinding();
            dualFactory = new DuplexChannelFactory<ServiceReference1.IDCCService>(new Callbacks(), binding, new EndpointAddress("net.pipe://localhost/DCCService"));
        }

        public void Open()
        {
            Proxy = dualFactory.CreateChannel();
        }

        public void Close()
        {
            dualFactory.Close();
        }
    }

    public class Callbacks : ServiceReference1.IDCCServiceCallback
    {
        void ServiceReference1.IDCCServiceCallback.OnCallback(string id, string message, Guid key)
        {
            Console.WriteLine(string.Format("{0}: {1}", id, message));
        }
    }
}

Here is the code for the working WCF console client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DCC_Client;

namespace Client_Console_Test
{
    class Program
    {
        private static DCCClient DCCClient;

        static void Main(string[] args)
        {
            try
            {
                DCCClient = new DCCClient();

                DCCClient.Open();

                DCCClient.Proxy.DCCInitialize(); //returns fine from here

                Console.ReadLine();
                DCCClient.Proxy.DCCUninitialize();

                DCCClient.Close();
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}

And here is the code for the WPF client that freezes (see comment)

using System; //etc

using DCC_Client;  //Used for connection to DCC Service

namespace Client_WPF_Test
{
    public partial class Main : Window
    {
        private static DCCClient DCCClient;

        public Main()
        {
            InitializeComponent();

            DCCClient = new DCCClient();

            DCCClient.Open();
        }

        private void Connect_btn_event() {

            try
            {
                DCCClient.Proxy.DCCInitialize(); //**never returns from this**
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

I entered the code DCCClient.Proxy.DCCInitialize();and the service successfully executed the commands, however for some reason the client is stuck here and does not continue to execute. The client does not throw an exception, and the stack trace says [external code].

, . , - . , .

+3
1

DCCInitialize, , . :

[CallbackBehavior(ConcurrencyMode=ConcurrencyModel.Reentrant)]

[OperationContract(IsOneWay=true)]

void

, :

[CallbackBehavior(UseSynchronizationContext=false)]

, .

Edit:

WCF -, . Windows, , , , , , , = , . , , WCF Windows , . , , UI , - WinForms, WPF .

+13

All Articles