English 中文(简体)
Silverlight 和 WCF:处理 WCF 向下异常 / WCF 连接检查。
原标题:Silverlight and WCF: Handle WCF down exception / WCF connection check

Inside my Silverlight app, I m calling a web service asynchronously, using the client generated for me by Visual Studio (Add Service Reference...).
My code looks like this:

ServiceClient serviceClient = new ServiceClient();
serviceClient.GetServerTimeCompleted += new EventHandler<GetServerTimeCompletedEventArgs>(serviceClient_GetServerTimeCompleted);
serviceClient.GetServerTimeAsync();

void serviceClient_GetServerTimeCompleted(object sender, GetServerTimeCompletedEventArgs e)
{
         // do nothing atm
}

只要服务正常运行,一切都很顺利,但是如果服务未运行,我会收到:

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

"An exception occurred during the operation, making the result invalid. Check 内部异常 for exception details."

内部异常

"An error occurred while trying to make a request to URI http:// . This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details."

在这些情况下出现异常是有道理的,但我无法弄清楚如何在客户端代码中捕获异常。有没有标准的方法来解决这个问题?如果我理解这是如何工作的,当我的代码调用生成的代码时,它会产生另一个线程,实际上调用Web服务并等待响应。异常在那个线程和生成的代码中。我的调用代码(见上文)和回调方法都没有机会看到异常。

谢谢 (Xièxiè)

基督教

问题回答

在Visual Studio中不使用“添加服务引用”功能有很多原因。这里有一篇很好(也很长)的文章讨论了这些问题,以及如何在创建客户端时使用WCF。我现在正在自己解决这些问题。

关于“添加服务引用”的担忧包括可下载客户端.XAP文件中的代码膨胀、难以维护生成的代码、当服务器地址和端口在客户端配置文件中硬编码时难以进行分阶段或发货操作(也嵌入在.XAP中),以及等等。构建自己的客户端也将使您更好地能够管理客户端代码中的异常。

有一些权衡需要考虑; 您需要使您的数据契约类可移植到 Silverlight 环境中。(您不能只是添加对服务器端 .NET 程序集的引用; 这行不通。) 上面链接的同一个网站有一篇关于在 Silverlight 中重新使用程序集的好文章,但处理这个问题的最简单方法可能是从 Silverlight 项目中添加链接到您的服务器端类。(在 Silverlight 项目中,右键单击并添加->添加现有项...,找到要包含的项,但不要单击添加按钮,单击旁边的下箭头,然后单击"作为链接添加")。注意其他引用和使用语句,这些语句可能引用服务器端类; 您可能需要进行条件编译,以便在客户端项目中清晰编译您的数据契约类。

总的来说,这看起来更加清洁,特别是对于可能使用大量服务合同和数据合同,或者您需要将Silverlight项目作为其他人服务器上运行的商业应用程序的一部分进行交付的大型项目。特别是对于集成和分期测试,这应该更加清洁易测。

我以前遇到过这个问题-您需要在您的ServiceClient安装代码周围添加异常处理代码。我通常检查以下CommunicationExceptionTimeoutExceptionEndpointNotFoundException并向用户提供一个适当的对话框,告诉他们某些东西出了问题:

ServiceClient serviceClient;
try
{
   serviceClient = new ServiceClient(); 
   serviceClient.GetServerTimeCompleted += new EventHandler<GetServerTimeCompletedEventArgs>(serviceClient_GetServerTimeCompleted); 
   serviceClient.GetServerTimeAsync();
}
catch (EndpointNotFoundException ex)
{
   // log exception
   // show error dialog
}

注意:此代码不会处理在异步方法回调中遇到的错误 - 它需要自己的(类似的)异常处理。

Actually you have to put crossdomainaccesspolicy.xml in IIS default website root folder. you copy the given below xml content and save in the name of ClinetAccespolicy.xml and then put on the IIS default website root folder.





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签