Álvaro García
Álvaro García

Reputation: 19366

WCF: callback contract no async, error because no implement Begin/End methods

I have a duplex WCF service. In the service contract, I have 3 async methods and 1 normal method (close session). In tha callback contract I only have 1 void method that is not async.

When I generate the proxy with svcUtil I use the /a paramter, and get the .cs. If I open the this file, I can see that is generated the Begin/end method for the no async method of the contract, I don't know well why, because this methods is not marked as async in the contract. Anyway, this is no the problem.

The problem is with the method of the callback contract. In my client application, I implement the callback interface, but I have implemented one method, an sync method, so I have not the Begin/End methods. However, I can't compile because I have an error, that the class that implement the callback interface does not implement the Begin/end methods.

Which is the problem?

Thanks. Daimroc.

Upvotes: 0

Views: 1771

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87248

The class which implements the callback interface needs to implement the asynchronous methods as well (since they're defined in the interface), but if an interface has both synchronous and synchronous versions of the same method, the synchronous one is invoked by default. In the code below, the asynchronous operations in the callback class just throw, but the code works just fine.

public class StackOverflow_10362783
{
    [ServiceContract(CallbackContract = typeof(ICallback))]
    public interface ITest
    {
        [OperationContract]
        string Hello(string text);
    }
    [ServiceContract]
    public interface ICallback
    {
        [OperationContract(IsOneWay = true)]
        void OnHello(string text);

        [OperationContract(IsOneWay = true, AsyncPattern = true)]
        IAsyncResult BeginOnHello(string text, AsyncCallback callback, object state);
        void EndOnHello(IAsyncResult asyncResult);
    }
    public class Service : ITest
    {
        public string Hello(string text)
        {
            ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            ThreadPool.QueueUserWorkItem(delegate
            {
                callback.OnHello(text);
            });

            return text;
        }
    }
    class MyCallback : ICallback
    {
        AutoResetEvent evt;
        public MyCallback(AutoResetEvent evt)
        {
            this.evt = evt;
        }

        public void OnHello(string text)
        {
            Console.WriteLine("[callback] OnHello({0})", text);
            evt.Set();
        }

        public IAsyncResult BeginOnHello(string text, AsyncCallback callback, object state)
        {
            throw new NotImplementedException();
        }

        public void EndOnHello(IAsyncResult asyncResult)
        {
            throw new NotImplementedException();
        }
    }
    public static void Test()
    {
        string baseAddress = "net.tcp://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new NetTcpBinding(SecurityMode.None), "");
        host.Open();
        Console.WriteLine("Host opened");

        AutoResetEvent evt = new AutoResetEvent(false);
        MyCallback callback = new MyCallback(evt);
        DuplexChannelFactory<ITest> factory = new DuplexChannelFactory<ITest>(
            new InstanceContext(callback),
            new NetTcpBinding(SecurityMode.None),
            new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();

        Console.WriteLine(proxy.Hello("foo bar"));
        evt.WaitOne();

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

Upvotes: 2

Related Questions