2017-01-21 9 views
2

サービスのデモコード:Wcfサービスで例外をスローしてクライアントにキャッチする方法はありますか?

public class Login : UserNamePasswordValidator 
    { 
     public override void Validate(string userName, string password) 
     { 
      if (new ProjectContext().Users.Count(x => x.Username == userName && x.Password == password) == 0) 
      { 
       throw new FaultException("Invalid login"); 
      } 
     } 
    } 

クライアントコードのデモ:

internal bool LoginOnWcf(string address, string password) 
     { 
      try 
      { 
       service.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; 
       service.ClientCredentials.UserName.UserName = address; 
       service.ClientCredentials.UserName.Password = password; 
       user = service.GetUserById(address); 
       return true; 
      } 
      catch (FaultException ex) 
      { 
       MessageBox.Show(ex.Message); 
       return false; 
      } 
     } 

のWeb.configデモ:

<behaviors> 
     <serviceBehaviors> 
     <behavior name="defaultProfile"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <serviceCredentials> 
      <clientCertificate> 
       <authentication certificateValidationMode="None" /> 
      </clientCertificate> 
      <serviceCertificate findValue="App1" storeLocation="CurrentUser" 
       storeName="My" x509FindType="FindBySubjectName" /> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="App1.App_Code.Authentication.Login, App_Code/Authentication"/> 
      </serviceCredentials> 
     </behavior> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
</behaviors> 

私はFaultExceptionを投げ、サービスcrashs 。 私はクライアント上のFaultExceptionを捕まえてサービスを継続したいと考えています。

私は自分の問題をうまく説明して欲しいと思います。

答えて

5

WCFは、エラーを使用してサーバー間でエラーをスローします。 FaultExceptionを使用して開始し、必要に応じて独自のカスタムフォールトを作成することができます。

あなたの例では、単純に次のようにすることができます。

throw new FaultException("Invalid login") 

このartcileは、WCFでの障害を達成する方法についての全体の多くの詳細を提供します。

+0

私の質問の更新を確認してください。実際に私は本当にクライアントの '例外 'をキャッチすることができていません – Elkin

+1

1.他の例外の代わりに問題の原因となっているのは、それがあなたのスローであると確信していますか? tryキャッチでメソッド全体をラップし、そこからフォルトを投げることを検討したいかもしれません。あなたは私の答えをもはや有効にしない質問を完全に編集しました。元の質問には理解不足がありませんでした。フォルトに関する新しい問題がある場合は、実際には別の質問をする必要があります。 –

+0

本当にすみません。問題は、 "この例外タイプがユーザ未処理のときに中断する"オプションが有効になっていることでした。 この[画像](https://www.google.pt/search?q=break+when+this+exception+type+is+user-unhandled&espv=2&biw=1600&bih=794&source=)で回答を改善することができます。この質問(http://stackoverflow.com/questions/16970642/visual-studio-not-breaking-on-user-unhandled-exceptions)は、次のように入力してください:lnms&tbm = isch&sa = X&ved = 0ahUKEwjJ0YvludnRAhVIwxQKHRW1BYQQ_AUIBigB#imgrc = lB-bsjrDrJqY9M%3A) ) – Elkin

関連する問題