来自Silverlight的简单WCF Web服务调用(需要帮助!)

时间:2010-05-17 08:49:43

标签: wcf silverlight

我是Silverlight编程和网络的新手,我一直在努力寻找答案。我正在尝试在VS2010中构建一个非常简单的Silverlight4 Web应用程序,该应用程序在ASP.NET网站上调用web服务(与Silverlight客户端应用程序完全相同的解决方案,因此不应存在任何跨域问题???)。我使用VS'“启用Silverlight的WCF服务”编写了一个Web服务,它只返回一个字符串。

然后我编写代码以在Silverlight中使用该服务:

public MainPage()         {             的InitializeComponent();

        TestServiceClient proxy = new TestServiceClient();
        EndpointAddress address = new EndpointAddress("http://localhost:" + HtmlPage.Document.DocumentUri.Port +
            "/SilverlightApplication1.web/TestService.svc");

        proxy.Endpoint.Address = address;
        proxy.GetStringCompleted += new EventHandler<GetStringCompletedEventArgs>(proxy_GetStringCompleted);
        proxy.GetStringAsync();

    }

    void proxy_GetStringCompleted(object sender, GetStringCompletedEventArgs e)
    {
        MessageBox.Show(e.Result.ToString());
    }

当我从VS2010运行时,这很有用。但是,当我将它发布到同一台计算机上的个人网络服务器(IIS7)时,我收到一个错误:

网页错误详情

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.1; Trident / 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0 ; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; .NET4.0C; .NET4.0E) 时间戳:星期一,2010年5月17日08:29:51 UTC

消息:Silverlight应用程序中的未处理错误操作期间发生异常,导致结果无效。检查InnerException以获取异常详细信息。在System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()    在SilverlightApplication1.TestServiceReference1.GetStringCompletedEventArgs.get_Result()    在SilverlightApplication1.MainPage.proxy_GetStringCompleted(Object sender,GetStringCompletedEventArgs e)    在SilverlightApplication1.TestServiceReference1.TestServiceClient.OnGetStringCompleted(对象状态) 线:1 Char:1 代码:0 URI:http://thunder.webhop.org:8001/home.html

当我在App.xaml.cs中捕获异常时,收到以下消息:

System.InvalidOperationException:Eval失败。 在System.Windows.Browser.HtmlWindow.Eval(字符串代码) 在 SilverlightApplication1.App.ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)

我应该提一下,网络服务器似乎工作正常。我可以通过端口8001在它上面托管Silverlight应用程序而没有任何问题。我不能为我的生活找出如何成功进行Web服务调用!任何有关这方面的帮助都将非常感激。我浪费了整整一个下午和晚上。我的思绪现在正在翻转......: - (

很多,非常感谢提前! 运气

P.S。在Silverlight论坛上交叉发布的道歉,但在发布时遇到了一些问题。

1 个答案:

答案 0 :(得分:0)

我想这实际上并不与Web服务调用有关,因为App.cs中捕获的异常说“Eval失败”。 我认为它可能是您的proxy_GetStringCompleted处理程序中的代码。 MessageBox.Show只不过是幕后的JavaScript警报。也许您正在使用激活了JavaScript阻止程序的浏览器等?无论如何;您可以尝试的是简单地使用除MessageBox.Show()之外的其他内容,例如

(new ChildWindow { Content = new TextBlock { Text = e.Result.ToString() } }).Show();

干杯,亚历克斯