【问题标题】:How do i call WCF services in Silverlight?如何在 Silverlight 中调用 WCF 服务?
【发布时间】: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 中,它确实会执行。可能是什么问题?

还请解释我这样做是否正确?我见过一个例子,一个人使用一些奇怪的符号直接定义函数,比如=&gt;

编辑 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


【解决方案1】:
  1. 您说得对,Silverlight 中的所有网络调用都是异步完成的。
  2. 您提到的=&gt; 语法是定义委托方法的简写,它称为lambda。 (见下文)
  3. 您应该能够在 Completed 事件处理程序中设置断点,如果不能尝试重新启动 Visual Studio(我之前已经看到它的行为很奇怪)。
  4. 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());  }; 

【讨论】:

  • 如果您使用的是 Linq-To-SQL,您需要通过 Linq-To-SQL 设计器在实体上设置序列化模式。选择表并查看其属性。序列化就是其中之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多