我有一个返回int的WCF数据服务[WebGet]方法。 (产品表中没有产品)
我如何阅读它的结果? 我使用浏览器检查了该方法。
public void ProductsCount()
{
ctx.BeginExecute<int>(new Uri(uriBase + "/GetNoProducts"), GetProductsCountCompleted, ctx);
}
public void GetProductsCountCompleted(IAsyncResult result)
{
inventory_db_bigEntities context=result.AsyncState as inventory_db_bigEntities;
var x = context.EndExecute<int>(result);
//how do i read the int out of the x variable
}
更新
也许BeginExecute和EndExecute不适合它。
在浏览器窗口中,webget方法返回:
“&lt; GetNoProducts p1:type =”Edm.Int32“&gt; 223863&lt; / GetNoProducts&gt;”
答案 0 :(得分:1)
IAsyncResult
的{{3}}使用BeginInvoke
而不是BeginExecute
,然后调用EndInvoke
来获取值:
// The asynchronous method puts the thread id here.
int threadId;
// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();
// Create the delegate.
AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
// Initiate the asychronous call.
IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);
...
// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
// Perform additional processing here.
// Call EndInvoke to retrieve the results.
string returnValue = caller.EndInvoke(out threadId, result);
答案 1 :(得分:0)
你的例子有什么问题? EndExecute应该做的伎俩...只需调用FirstOrDefault()来获取原始值。或者,如果您不使用SL,则可以同步执行:
http://msdn.microsoft.com/en-us/library/hh230677.aspx#ExecutePrimitiveValue