TomCaps
TomCaps

Reputation: 2527

Duplex communication with net.tcp: How to get return value from CallbackContract

//WCF Services Code:
    [ServiceContract(Namespace = "Silverlight", CallbackContract = typeof(IStockServiceClient))]
    public interface IStockService
    {
        [OperationContract(IsOneWay = false)]
        void Connect();
    }

    [ServiceContract]
    public interface IStockServiceClient
    {
        [OperationContract(IsOneWay = true)]
        int SendUpdate(string value);
    }

    public class StockService : IStockService
    {
        private static IStockServiceClient client;
        public void Connect()
        {
            client = OperationContext.Current.GetCallbackChannel<IStockServiceClient>();
        }
        public static IStockServiceClient Client
        {
            get
            {
                return client;
            }
        }
    }

//Call Silverlight Code
    private void button1_Click(object sender, RoutedEventArgs e)
    {
       var result = DuplexStockService.Web.StockService.Client.
                SendUpdate("test");
    }


//silverlight code
   private void button1_Click(object sender, RoutedEventArgs e)
   {
            ServiceReference1.StockServiceClient client =
                new ServiceReference1.StockServiceClient();
            client.ConnectAsync();
            client.SendUpdateReceived += (senderSendUpdateReceived, eSendUpdateReceived) =>
                {
                    //return 1; //Doesn't work
                    //How to return a value back to WCF?
                };
   }

The callback method int SendUpdate(string value) is supposed to return an int value,but how can the silverlight's SendUpdateReceived event handler return an int value back to its wcf callee?

Upvotes: 2

Views: 470

Answers (2)

Sander
Sander

Reputation: 26374

What you are asking is not supported in Silverlight - the callbacks can only be one-way operations that do not return values. This is an additional restriction placed on duplex services by Silverlight - in non-Silverlight WCF, you can specify any return type that you desire.

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

You cannot return any value if your operation contract is marked as OneWay. All one way operations must return void because the client never waits for their response - that's why they are called one way.

Upvotes: 1

Related Questions