Wednesday, September 2, 2015

Exception handling when using WCF-services

Surely you are faced with a situation where if an error occurs in a WCF-service to be transmitted to the client any data or, for example, a detailed description of what happened. Very often there are implementation WCF- services using objects returned to the client with additional used only in the event of an error, fields, even if the implementation of the method does not require the return of any result to the client. So do because if you just throw an exception, the client receives almost no information about what happened. Imagine that we have a WCF service that's so easy to contract and implementation.


?
1
2
3
4
5
6
7
8
9
10
11
12
13
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//контракт</span> // contract</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[ServiceContract]</span> [ServiceContract]</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public interface IDataService</span> public interface IDataService</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>       
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[OperationContract]</span> [OperationContract]</span>       
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">void TestError();</span> void TestError ();</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//реализация</span> // implementation</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public void TestError()</span> public void TestError ()</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">throw new Exception("Error!");</span> throw new Exception ("Error!");</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>

As if to realize the service is so, then the client will have an exception that does not give any details about the incident:

You can, of course, as it is written in the error, turn IncludeExceptionDetailInFaults, as it is written in the error message. But this property is not recommended except for debugging, as this client in the event of any error to get all the information about the call stack and the structure of your service, which is very bad in terms of safety.
But, in addition to options for debugging and architecture is not the best option with the added field as a result of a call, there is a third option - a more convenient and more correct.

This use FaultContract and FaultException. Let's make a deal, and the implementation of our services with the use of these opportunities WCF.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[ServiceContract]</span> [ServiceContract]</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//контракт</span> // contract</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public interface IDataService</span> public interface IDataService</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>       
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[OperationContract]</span> [OperationContract]</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[FaultContract(typeof(ServiceError))]</span> [FaultContract (typeof (ServiceError))]</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">void TestError();</span> void TestError ();</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[DataContract]</span> [DataContract]</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public class ServiceError</span> public class ServiceError</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[DataMember]</span> [DataMember]</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public int ErrorCode { get;</span> public int ErrorCode {get;</span> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">set;</span> set;</span> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">[DataMember]</span> [DataMember]</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public string Message { get;</span> public string Message {get;</span> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">set;</span> set;</span> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>       
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//реализация</span> // implementation</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public void TestError()</span> public void TestError ()</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">ServiceError srvError = new ServiceError();</span> ServiceError srvError = new ServiceError ();</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">srvError.ErrorCode = 123;</span> srvError.ErrorCode = 123;</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">srvError.Message = "Error!";</span> srvError.Message = "Error!";</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">throw new FaultException<ServiceError>(srvError);</span> throw new FaultException <ServiceError> (srvError);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>

Now, if the client does not catch the exception and just FaultException <>, you can get all the information that we gave to the service.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"> using (TestService.TestServiceClient client = new TestService.TestServiceClient())</span> using (TestService.TestServiceClient client = new TestService.TestServiceClient ())</span>
               <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
                   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">try</span> try</span>
                   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>                                               
                       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">client.TestError();</span> client.TestError ();</span>
                   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
                   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">catch (FaultException<TestService.ServiceError> ex)</span> catch (FaultException <TestService.ServiceError> ex)</span>
                   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
                       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//поля вашего класса, объявленного как FaultContract, будут находится в свойстве исключения Datail</span> // your class field declared as FaultContract, will be in the property exception Datail</span>
                       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//в данном случае появится сообщение "123Error!";</span> // in this case, the message "123Error!";</span>
                       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">MessageBox.Show(ex.Detail.ErrorCode.ToString()+ex.Detail.Message);</span> MessageBox.Show (ex.Detail.ErrorCode.ToString () + ex.Detail.Message);</span>
                   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
               <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>

In principle, everything should work fine with one exception - when debugging in Visual Studio, you may receive strange error System.ServiceModel.FaultException`1 was unhandled by user code on the spot in the service, where you throw an exception:

Some tips on the Internet say that to repair it is necessary to completely disable Exception assistant (then generally no errors will not be caught by the studio), and other settings in this same assistant general disable all CLR Exceptions (which, in the context of the development on the .Net equivalent previous version). In fact, one is enough to disable the exception and everything will work. Go to the Debug -> Exception, open category Common Language Runtime Exceptions, and then System.ServiceModel remove the tick System.ServiceModel.FaultException`1.
And everything will work fine. 

By Unknown with No comments

0 коммент.:

Post a Comment