2014-07-08 174 views
0

‘期間System.ServiceModel.EndpointNotFoundException’異常情況發生,我創造,在C#中消耗了SOAP Web服務的應用程序。我生成的代理類使用svcutil工具Web服務的WSDL。捉「異步Web服務調用

我加入了代理類,以我的代碼,我用它來向Web服務調用和asynchrounsly得到結果。當客戶端有一個上網

一切都工作得很好。但是,如果我跑企圖訪問而應用程序沒有Internet訪問它崩潰引發以下異常:

An exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in 
System.ServiceModel.Internals.dll but was not handled in user code 

我試圖捕獲這個異常阻止應用程序崩潰,併爲用戶提供一個更友好的錯誤消息,但是,因爲我做的異步網絡電話,只要周圍由try Web服務調用 - catch沒有幫助。

根據異常詳細信息,它發生在自動生成的代理文件中定義的End_FunctionName函數中。

有關如何能夠優雅地處理此異常的任何提示?

+1

確保您呼叫正在返回任務或任務而不是**無效**否則你將無法趕上你的異步方法在嘗試抓住時例外。 [MSDN Mag:異步編程中的最佳實踐](http://msdn.microsoft.com/zh-cn/magazine/jj991977.aspx) – MickyD

回答

0

很難確切地知道發生了什麼;不過,我會假設你有一個像這樣的

[ServiceContract] 
public interface IMyService 
{ 
    [OperationContract] 
    String Hello(String Name); 

    [OperationContract] 
    Person GetPerson(); 
} 

Web服務,你可能會有這樣的代理:

public class MyPipeClient : IMyService, IDisposable 
{ 
    ChannelFactory<IMyService> myServiceFactory; 

    public MyPipeClient() 
    { 
     //This is likely where your culprit will be. 
     myServiceFactory = new ChannelFactory<IMyService>(new NetNamedPipeBinding(), new EndpointAddress(Constants.myPipeService + @"/" + Constants.myPipeServiceName)); 
    } 

    public String Hello(String Name) 
    { 
     //But this is where you will get the exception 
     return myServiceFactory.CreateChannel().Hello(Name); 
    } 

    public Person GetPerson() 
    { 
     return myServiceFactory.CreateChannel().GetPerson(); 
    } 

    public void Dispose() 
    { 
     ((IDisposable)myServiceFactory).Dispose(); 
    } 
} 

如果您在連接你會得到它不是你當錯誤嘗試連接到頻道工廠,但實際嘗試調用某個功能時。

要解決這個問題,你可以嘗試一下抓住每一個函數調用並手動處理異步調用。相反,您可以擁有像init()這樣的函數,每次您實例化連接時都會同步調用該函數。這樣你就知道,如果這個呼叫連接你有一個連接。

如果您在任何時候有連接丟失的風險,我建議您使用以前的選項。

反正這裏是你如何解決這個問題的例子:我上傳了我寫的,這樣你可以下載完整的源代碼源

public class MyPipeClient : IMyService, IDisposable 
{ 
    ChannelFactory<IMyService> myServiceFactory; 

    public MyPipeClient() 
    { 
     myServiceFactory = new ChannelFactory<IMyService>(new NetNamedPipeBinding(), new EndpointAddress(Constants.myPipeService + @"/" + Constants.myPipeServiceName + 2)); 
    } 

    public String Hello(String Name) 
    { 
     try 
     { 
      return Channel.Hello(Name); 
     } 
     catch 
     { 
      return String.Empty; 
     } 
    } 

    public Person GetPerson() 
    { 
     try 
     { 
      return Channel.GetPerson(); 
     } 
     catch 
     { 
      return null; 
     } 
    } 

    public Task<Person> GetPersonAsync() 
    { 
     return new Task<Person>(()=> GetPerson()); 
    } 

    public Task<String> HelloAsync(String Name) 
    { 
     return new Task<String>(()=> Hello(Name)); 

    } 

    public void Dispose() 
    { 
     myServiceFactory.Close(); 
    } 

    public IMyService Channel 
    { 
     get 
     { 
      return myServiceFactory.CreateChannel(); 
     } 
    } 
} 

。你可以在這裏得到它:https://github.com/Aelphaeis/MyWcfPipeExample

PS:這個知識庫引發你得到的異常。爲了刪除它只需去MyPipeClient並刪除構造函數中的+ 2。

如果您使用的是雙面打印,可考慮使用該庫: https://github.com/Aelphaeis/MyWcfDuplexPipeExample