//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?
};
}
回调方法int SendUpdate(字符串值)应该返回一个int值,但是silverlight的SendUpdateReceived事件处理程序如何将一个int值返回给它的wcf被调用者?
答案 0 :(得分:1)
如果您的操作合同标记为OneWay
,则无法返回任何值。所有单向操作必须返回void
,因为客户端永远不会等待他们的响应 - 这就是为什么它们被单向调用的原因。
答案 1 :(得分:1)
Silverlight不支持您所要求的内容 - 回调只能是不返回值的单向操作。这是Silverlight对双工服务的额外限制 - 在非Silverlight WCF中,您可以指定所需的任何返回类型。