WCF : FaultContract(typeof(ExceptionDetail)) issue - wcf

I have put the attribute [FaultContract(typeof(ExceptionDetail))] for my operation contract. When I am trying to add the service to a client application, I get this error - "Custom tool error: Failed to generate code for the service reference 'ServiceReference1'. Please check other error and warning messages for details."
But when I comment out the FaultContract Attribute, I am able to add the wcf service reference th' my client app.

The point of having FaultContracts is to make it possible to first of all pass back SOAP faults from the service which will not break the communication channel between the server and the client (handling error conditions like .NET exceptions gracefully and interoperably), and secondly, using FaultContracts, your server than throw typed faults (FaultException<T>) and your client can catch those.
If you want or need to be really interoperable, you need to:
define all your FaultContract types as classes decorated with the [DataContract] attribute
catch all .NET exceptions on the server (using e.g. IErrorHandler interface) and turn them into interoperable SOAP faults
If you control both ends of the wire and both ends are .NET, then you can simplify this by one step: on the server, handle all .NET exceptions and turn them into e.g. FaultException<ArgumentOutOfRangeException>, that is, create a "fault of (whatever .NET exception)" and then on the client, catch those typed FaultException and handle them:
[FaultContract(typeof(ArgumentOutOfRangeException)]
[OperationContract]
public void CallService(.......)
and then in your implementation, use this:
try
{
clientProxy.CallService();
}
catch(FaultException<ArgumentOutOfRangeException> ex)
{
// handle the most specific exception first
}
catch(FaultException ex)
{
// handle all other, unspecific server faults
}
catch(CommunicationException ex)
{
// handle all other, client-proxy related WCF errors
}
catch(Exception ex)
{
// handle anything else....
}

Remove that FaultContract, and instead configure includeExceptionDetailInFaults:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

Use Service Trace Viewer tool from http://msdn.microsoft.com/en-us/library/ms732023.aspx, to view the activity trace .

I had the same problem few minutes ago.
It was due to the absence of a default constructor. Also remember that all properties must have public get/set accessors.

Related

Why we need Fault Contracts in WCF

I have been getting my hands Dirty in WCF. Now one of the question which comes to my mind is regarding Fault Contracts in WCF.
I would like to know why we would be needing it. Consider a Sample Application where I am adding 2 Numbers.
So in the Response I have like 2 Fields
Result - Success/Error
Error - Error Details (Code + Text)
Now if my WCF service had any Exception I can catch it in the catch block and assign values to Response object
Result - Success/Error
Error - Error Details (Code + Text)
So where does Fault Contract come into the picture?
What you are doing in your example is you're indicating to the caller that an error has occurred via a "return code". Fault Contracts represent a different approach to doing this: exceptions.
There are many reasons why exceptions are considered better than return codes. Read this for example: Which, and why, do you prefer Exceptions or Return codes? . This is why WCF's architects chose to provide the Fault Contract mechanism, rather than implement the same functionality via return codes.
In your case the Fault Contract approach would mandate that you shouldn't return a response object. You should simply return an int. If anything exceptional happens that prevents you from returning the int, your code would throw a strongly typed Fault indicating to the caller what went wrong and how to, possibly, overcome it.
This is a old question, but I still wish to post some answers for future readers.
Found this website http://www.c-sharpcorner.com/UploadFile/aravindbenator/wcf-exception-handling-best-ways/.
The Author said, if we do not use Fault Contract, the response data (from service to client) will include some sensitive data.
If we do not have Fault Contract, in WCF app.config or web.config, and we still want Fault Exceptions or Web Fault Exceptions, we will set as:
<serviceDebug includeExceptionDetailInFaults="true" />, however, if we set <serviceDebug includeExceptionDetailInFaults="false" />, we must have Fault Contract above service operations.

WCF IErrorHandler.ProvideFault() behavior

My service methods are marked with PrincipalPermissionAttribute and i have a custom IErrorHandler implementation attached to the service. When an incoming request has no permissions to execute the method System.Security.SecurityException is thrown. IErrorHandler.ProvideFault() is then triggered and i want to provide a special fault. But error parameter is not original exception, it's untyped FaultException. Moreover, error.InnerException is null despite i have the following setting in the config:
<serviceDebug includeExceptionDetailInFaults="true"/>
Why? How can i achieve the desired behavior?
According to this SecurityException is kind of "special" for WCF:
SecurityException is related to CAS
(Code Access Security), and it is a
fatal exception. Since this exception
is not related to any service model
exceptions, it cannot be handled by
IErrorHandler.
So i ended up by creating additional class which methods (called from the methods of the class implementing the service interface) are marked with PrincipalPermissionAttribute and a handler function with try/catch.

How do I get a end-user friendly message to propagate when an the wrong type parameter is passed to a WCF web service?

Let say I have a WCF
Foo(int param);
The client is passing in a JSON string. Instead of passing in an integer, they pass in a string.
The system now returns a 500 error back to the client. The event log says that I need to add includeExceptionDetailInFaults="true" to my config file if I want a friendly message to be returned. I go and do that but then I still get the 500 error and an event log error stating that I cannot add the 'serviceDebug' extension to my endpoint behavior because the underlying behavior type does not implement the IEndpointBehavior.
What does that suppose to mean?
First of all: where did you add the <serviceDebug> behavior, and how? Can you show us? The <serviceDebug> needs to be added to the <serviceBehavior> section on your server - not the endpoint behavior section. It's a service behavior, after all (it affects the whole service) - not an endpoint behavior (which affects only a single endpoint but not others).
So you should have:
<serviceBehaviors>
<behavior name="debug">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
in your server-side config (web.config or app.config), and then apply that service behavior to your service tag:
<services>
<service name="...."
behaviorConfiguration="debug">
....
Secondly: error 500 is an internal server error, so this means, the server couldn't interpret and handle your input. The best bet would be to do some client-side validation before actually sending this input to the service, to avoid these kind of errors.
If you cannot do this, then maybe you need to add some more logic to your service so you can capture and figure out these kind of errors before they blow up your service code.
And thirdly, the ultimate solution: you could write a client-side parameter inspector to catch these wrong parameters even before they're being sent to the server, and react accordingly. WCF is very extensible that way. See the MSDN How To Inspect Or Modify Parameters or this blog post if you're interested in learning more about parameter inspectors.

Trouble using IAuthorizationPolicy with WCF and Castle-Windsor

I'm currently using Castle-Windsor with the WCF Facility to inject all my WCF services. I've just started adding permission requirements using a custom IAuthorizationPolicy, which seems to work when done on a per-method basis on the service, but when the service class itself is marked up with the requirements, I get an exception thrown.
I've set things up based on the example at How To – Use Username Authentication with Transport Security in WCF from Windows Forms. I didn't set up the custom HTTP Module class as I'm using an existing Membership implementation. My IAuthorizationPolicy implementation (HttpContextPrincipalPolicy) is essentially identical.
The essential part of my Web.config is:
<serviceBehaviors\>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceAuthorization principalPermissionMode="UseAspNetRoles"
roleProviderName="UserProvider">
<authorizationPolicies>
<clear/>
<add policyType="Website.FormsAuth.HttpContextPrincipalPolicy,Website"/>
</authorizationPolicies>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
Everything seems to work fine when I put the requirements on the method. This is being done like so:
[PrincipalPermission(SecurityAction.Demand, Role = RoleNames.USER_ADMINISTRATION)]
If this is on an OperationContract method, things work as expected. However, if it is moved to the class itself (which implements the ServiceContract) I get the following exception (with most of the extra stuff pruned out):
Castle.MicroKernel.ComponentActivator.ComponentActivatorException {
Message = "ComponentActivator: could not instantiate Services.UserService"
InnerException = System.Reflection.TargetInvocationException {
Message = "Exception has been thrown by the target of an invocation."
InnerException = System.Security.SecurityException {
Message = "Request for principal permission failed."
}
}
}
I've debugged and found that the constructor on HttpContextPrincipalPolicy is being called but Evaluate() is not when the demand is attached to the class. When it is attached to the method Evaluate() is being called. So at this point I've gone as far as my newbie .NET/WCF/Castle-Windsor skills will take me.
Is there a way to tell Castle-Windsor to invoke the service constructor while honoring the IAuthorizationPolicy? Or tell WCF that Evaluate() needs to be called for the creation of the class? Or is there some other way around WCF that does the same thing? I don't want to have to mark up every single method with the exact same bit of attribute declaration.
When you mark the class itself up with a PrincipalPermissionAttribute it's effectively saying to the runtime that at the point when the class is used the permission demand must be met. So now when Castle-Windsor is trying to instantiate the class, the permission demand is being made and of course it can't be fulfilled because the security context isn't established correctly at that point.
AFAIK, PrincipalPermissionAttribute is not supported on the class level for WCF due to the nature of its runtime even though it is allowed from a pure .NET perspective. Castle-Windsor is therefore unable to create your service instance for the same reason.

WCF Error Logging

I am working on a project that is using NetTcp bindings for talking to some WCF services. When first starting out we took a bad practice example from another application that made WCF calls by using the following:
EntityObject sample = new ServiceProxy().GetEntity();
That worked great until we realized WCF was holding onto the connections even though the aspx page had been sent to the client (which I naively assumed would clean up any connections). While the connections were held on causing things to eventually slow down, ELMAH logged any errors and sent us full stack traces. To resolve the performance issues we changed to this:
using (ServiceProxy proxy = new ServiceProxy())
{
sample = proxy.GetEntity();
}
This made performance rock comparatively. The downside to this method is whenever an error is received on the proxy the only thing ELMAH can catch is that the channel is now faulted. We then have to dig through logs (the WCF ones setup with sharedListeners in ) to figure out what happened and if it's a serialization error the odds of actually find it become much lower, despite the listeners being setup on both client and server. I've explored the IErrorHandler interface and am going to add support for it to our services, but I was wondering if there are other ways to get detailed errors out of WCF instead of it just saying it faulted with no real information as to why it faulted. This would be especially beneficial if it dies on serializing the object that it could tell us WHY it couldn't serialize.
I think if you call Close() explicitly on the proxy, and put that in a try-catch, you'll get what you want.
See especially this sample:
http://msdn.microsoft.com/en-us/library/aa355056.aspx
Well, you can tell the WCF servive to send back more information than just "something bad happened" by using the serviceDebug behavior.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ExceptionDetails">
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
This is OK as long as it's a dev/test environment, or an in-house app. But really, service error should be caught (and logged) on the server side - you're on the right path with the IErrorHandler interface.
The client needs to handle client-side exceptions, like TimeoutException and CommunicationException to deal with security exceptions or networks going down and such. Just standard .NET exception handling, really.
Also, the using() is a good idea in general - but not necessarily here, since you could encounter an exception when the ServiceProxy is being disposed of (at the end of the using() {} block), and that won't be caught in this case. You might just need to use a try {...} catch {...} finally {...} block for your service proxies instead.
Marc