【发布时间】:2010-08-25 00:54:48
【问题描述】:
谁能告诉我为什么这不起作用。我创建了一个 WCF 服务,它从 Northwind 数据库返回客户列表。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFSilverlight.Web
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Customers" in code, svc and config file together.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Customers : ICustomers
{
IEnumerable<Customer> ICustomers.GetAllCustomers()
{
NorthwindEntities objNorthwindEntities = new NorthwindEntities();
var query = from cust in objNorthwindEntities.Customers
select cust;
return query.ToList();
}
}
}
这是我的 App.xaml.cs 代码片段:-
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
CustomersClient objCustomersClient = new CustomersClient();
objCustomersClient.GetAllCustomersCompleted += new EventHandler<GetAllCustomersCompletedEventArgs>(client_GetNameCompleted);
objCustomersClient.GetAllCustomersAsync();
}
void client_GetNameCompleted(object sender, GetAllCustomersCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
如果我没记错的话,Silverlight 中的方法是异步调用的。所以我添加了一个事件处理程序来处理它,然后调用该方法来检索客户。但我在 Messagebox 中没有得到任何东西。此外,当我尝试在client_GetNameCompleted 上保留断点时,它永远不会执行。但如果我将它保存在Application_Startup 中,它确实会执行。可能是什么问题?
还请解释我这样做是否正确?我见过一个例子,一个人使用一些奇怪的符号直接定义函数,比如=>。
编辑 1:- 请解释一下 e.UserState 中的 e.UserState 是什么。它包含什么,我可以用它做什么?
编辑 2 :- :- 我收到此错误 http://img178.imageshack.us/img178/9070/53923202.jpg
WCF 服务运行良好我已经测试了链接查询。所以 Sql Server 连接或 WCF 没有问题。只是我的客户出了点问题。
这是我的 ServiceReference.ClientConfig :-
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICustomers" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:50622/Customers.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICustomers" contract="CustomerServ.ICustomers"
name="BasicHttpBinding_ICustomers" />
</client>
</system.serviceModel>
</configuration>
你现在能告诉我哪里出了问题吗?
提前致谢:)
更新:- 我在 google 中看到您需要将序列化模式设置为单向。但是我在哪里设置呢?我在哪里写什么?
【问题讨论】:
-
e.Result 应该是 IEnumerable
类型,您可能需要将其转换回。 -
最后我在设计器中进行了延迟加载,它成功了:)
标签: silverlight wcf