Note that there are some explanatory texts on larger screens.

plurals
  1. POError handling in RESTful WCF service in IIS 7.5 with .NET 3.5
    primarykey
    data
    text
    <p>I have a REST WCF web service (.NET 3.5, not using REST Starter Kit) I'd like to create a custom error handler for. It's running as a .svc file in a web application through IIS 7.5. <strong>EDIT:</strong> Probably also worth mentioning; it's running over HTTPS.</p> <p>The main service class in the .svc.cs file:</p> <pre class="lang-cs prettyprint-override"><code>[ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class Service { [WebGet(UriTemplate = "/units/{id}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json), OperationContract] public Unit GetUnit(string id) { ... } ... } </code></pre> <p>web.config:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;system.serviceModel&gt; &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt; &lt;services&gt; &lt;service name="WebReservationService.Service"&gt; &lt;endpoint address="" binding="webHttpBinding" contract="WebReservationService.Service"/&gt; &lt;/service&gt; &lt;/services&gt; &lt;/system.serviceModel&gt; &lt;connectionStrings configSource="WebSettingsConnectionStrings.config"/&gt; &lt;system.web&gt; &lt;compilation debug="true"/&gt; &lt;/system.web&gt; &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"&gt; &lt;add name="AuthModule" type="WebReservationService.AuthModule"/&gt; &lt;/modules&gt; &lt;/system.webServer&gt; &lt;appSettings configSource="WebClientAuthentication.config"/&gt; &lt;/configuration&gt; </code></pre> <p>I tried to create an IErrorHandler class and attempted to use that through web.config but wasn't able to get it to trigger. Also tried to catch errors through an IHttpModule Error handler, but that doesn't trigger either. Currently I'm catching errors manually in each WebGet/WebInvoke but that doesn't catch serialization errors or unhandled URIs and the like, not to mention it adds needless clutter to the code.</p> <p>I know there's something obvious I'm missing, but the answers I've come across don't seem to be usable for my configuration. Any suggestions?</p> <p>Thanks.</p> <p><strong>EDIT:</strong> Just tried using a custom WebServiceHostFactory to apply a custom WebHttpBehavior. The factory and behavior are both run and the error handler is being created fine - HandleError and ProvideFault are just never being called:</p> <pre class="lang-cs prettyprint-override"><code>public class ServiceFactory : WebServiceHostFactory { public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) { ServiceHost host = CreateServiceHost(typeof(Service), baseAddresses); host.Description.Endpoints[0].Behaviors.Add(new ServiceBehavior()); return host; } protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { return base.CreateServiceHost(serviceType, baseAddresses); } } public class ServiceBehavior : WebHttpBehavior { protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear(); endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ServiceErrorHandler()); } } public class ServiceErrorHandler : IErrorHandler { public bool HandleError(Exception error) { return true; } public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault) { throw new NotImplementedException(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload