Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Web服务连接问题_C#_.net_Web Services_Exception Handling_App Config - Fatal编程技术网

C# .NET Web服务连接问题

C# .NET Web服务连接问题,c#,.net,web-services,exception-handling,app-config,C#,.net,Web Services,Exception Handling,App Config,我目前正在开发一个用WPF设计的软件,它连接到一组Web服务。Web服务在我的应用程序的app.config:endpoint中定义: <endpoint address="http://localhost:52870/WebServices/Service.svc" behaviorConfiguration="VanguardWebServiceBehavior" binding="wsHttpBinding" bindingConfiguration="WSHttpBind

我目前正在开发一个用WPF设计的软件,它连接到一组Web服务。Web服务在我的应用程序的app.config:endpoint中定义:

<endpoint address="http://localhost:52870/WebServices/Service.svc" 
  behaviorConfiguration="VanguardWebServiceBehavior" binding="wsHttpBinding"
  bindingConfiguration="WSHttpBinding_IService" contract="WebServices.IService"
    name="WSHttpBinding_IService">

在应用程序设置中:

<applicationSettings>
 <MyApp.Properties.Settings>
  <setting name="PrimaryRS" serializeAs="String">
    <value>http://localhost:50563/</value>
  </setting>
  <setting name="PrimaryWS" serializeAs="String">
    <value>http://localhost:52870/WebServices/Service.svc</value>
  </setting>
  <setting name="SecondaryWS" serializeAs="String">
    <value>http://localhost:52870/WebServices/Service.svc</value>
  </setting>
 </MyApp.Properties.Settings>
</applicationSettings>

http://localhost:50563/
http://localhost:52870/WebServices/Service.svc
http://localhost:52870/WebServices/Service.svc
只要WebServices是可访问的,这就可以正常工作,但是当WebServices不可访问时,应用程序就会崩溃。我想知道是否有任何方法可以捕获当WebServices不可访问时抛出的异常,并简单地以某种断开连接的状态打开应用程序,这样就没有任何内容是可编辑的

提前感谢,


Patrick

从您的配置中,看起来您正在使用WCF-对吗

在这种情况下,基本上只需将web服务调用包装在
try…catch
块中:

try
{ 
    WebServiceClient client = new WebServiceClient();
    client.CallSomeMethod();
}
catch(EndpointNotFoundException exc)
{
   // endpoint not found --> webservice is not up and running
}
.....  
catch(CommunicationException exc)
{
   // base WCF exception - for things like config errors etc.
}
在这种情况下,您可以捕获可能发生的特定异常并保持静默工作,向用户显示一个消息框,或者在这种情况下您希望执行的任何操作

几乎所有的WCF异常都是
CommunicationException
的后代(请参阅),因此捕获应该可以处理大多数WCF错误


如果您希望对向您发送错误的服务作出反应,您可能还希望捕获
故障异常
(在
通信异常
之前)-这些可能是服务器上执行可能导致的错误(例如,无法连接到数据库或后端或其他)所以我终于找到了这个问题的症结所在

要在应用程序加载时捕获EndpointNotFoundException,初始连接的所有try/catch逻辑必须在App.xaml.cs中,而不是在MainPage.xaml.cs中

我在MainPage.xaml.cs中有自动登录信息和WebService连接部分,但没有发现错误。将登录和Web服务连接从MainPage.xaml.cs移到App.xaml.cs后,我能够轻松捕获Web服务连接故障


希望这对其他人有所帮助。

感谢您的快速回复,但是我所有的Web服务呼叫都按照您的建议打包。我收到的异常是一个XAMLParseException,它发生在我编写的任何C#代码之前。我在应用程序的最开始添加了一个“throw new Exception();”来确定这一点。