有谁可以告诉我为什么这不起作用。我创建了一个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中的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中读到你需要将序列化模式设置为单向。但我在哪里设置这个?我在哪里写的?
答案 0 :(得分:2)
=>
语法是定义委托方法的简写,称为lambda。 (见下文)e.UserState
将引用您在异步调用的UserState变量中放入的任何对象(请注意额外的重载)。代码:
objCustomersClient.GetAllCustomersCompleted = delegate(object Sender, GetAllCustomersCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
};
// is the same as
objCustomersClient.GetAllCustomersCompleted += new EventHandler<GetAllCustomersCompletedEventArgs>(client_GetNameCompleted);
void client_GetNameCompleted(object sender, GetAllCustomersCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
// which is same as
objCustomersClient.GetAllCustomersCompleted += (sender, e) => { MessageBox.Show(e.Result.ToString()); };