【问题标题】:My silverlight Application Can't call the WCF service我的 silverlight 应用程序无法调用 WCF 服务
【发布时间】:2011-04-08 14:56:13
【问题描述】:

我使用 WCF 服务创建了一个简单的 silverlight 应用程序。我的服务创建得很好,我还在 wcftestclient 的帮助下测试了 WCF 服务,它工作正常。我也提供了 silverlight 的服务参考,但是当我运行 silverlight应用程序我无法得到结果。

这是silverlight代码

    void getdatacs(object sender, ServiceReference1.GetDataCompletedEventArgs e)
    {
        MessageBox.Show(e.Result);
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
        cs.GetDataCompleted += new EventHandler<ServiceReference1.GetDataCompletedEventArgs>(getdatacs);
        cs.GetDataAsync(Convert.ToInt32(textBox1.Text));
    }

这是我的 WCF 代码

    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

任何人都可以告诉我你无法得到结果。

【问题讨论】:

  • 你有异常吗?
  • 没有。我运行 SL 应用程序,它简单地等待浏览器底部没有任何反应,它说 Connection to localhost...
  • 你试过同步调用方法吗?运行 Silverlight 时服务是否正在运行?
  • 是的,你说得对。但调用是异步的...
  • 你我不知道结果...

标签: silverlight-3.0 wcf


【解决方案1】:

服务是否与 SL 应用程序分开托管?如果是这样,您可能需要添加 clientaccesspolicy 文件。见this link for details

【讨论】:

    【解决方案2】:

    代码看起来正确,所以我将开始查看 WCF 服务的配置。仔细检查您对 WCF 服务的引用中的所有设置。我相信您的问题不在代码中,而在其他地方。尝试将输入的 int 值删除到 WCF 服务并将字符串中的任何值发回,以消除任何输入问题的可能性,尽管它看起来没问题。

    我遇到过这样的问题,结果通常是在 WCF 服务的配置中。

    【讨论】:

    • 我查看了我的一个项目,在我的 WCF 服务 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 的公共类行上方有这行代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多