Note that there are some explanatory texts on larger screens.

plurals
  1. POCant get Global errorhandler to work on my selfhosting wcf service
    primarykey
    data
    text
    <p>Hello i've tried to get the Ierrorhandler to work now for hours and im pretty stuck :) i've gotten the most results out of this guide </p> <p><a href="http://www.remondo.net/wcf-global-exception-handling-attribute-and-ierrorhandler/#comment-10385" rel="nofollow">http://www.remondo.net/wcf-global-exception-handling-attribute-and-ierrorhandler/#comment-10385</a></p> <p>but i cant get it to work with my opertations/functions </p> <p><strong>Program.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Phpwcfconsole { class program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(Service1))) { try { host.Open(); Console.WriteLine("Host open. Press any key to &lt;EXIT&gt;"); Console.ReadLine(); host.Close(); } catch (Exception e) { Console.WriteLine(Environment.NewLine + e.Message); host.Close(); } } } } } </code></pre> <p><strong>app.config</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;system.web&gt; &lt;compilation debug="true" /&gt; &lt;/system.web&gt; &lt;system.serviceModel&gt; &lt;services&gt; &lt;service behaviorConfiguration="Phpwcfconsole.Service1Behavior" name="Phpwcfconsole.Service1"&gt; &lt;endpoint address="" binding="basicHttpBinding" contract="Phpwcfconsole.IService"&gt; &lt;identity&gt; &lt;dns value="localhost" /&gt; &lt;/identity&gt; &lt;/endpoint&gt; &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt; &lt;host&gt; &lt;baseAddresses&gt; &lt;add baseAddress="http://agent007:8732/phpwcf/" /&gt; &lt;/baseAddresses&gt; &lt;/host&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="Phpwcfconsole.Service1Behavior"&gt; &lt;serviceMetadata httpGetEnabled="True"/&gt; &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; &lt;bindings&gt; &lt;basicHttpBinding&gt; &lt;binding name="MyServiceBinding" hostNameComparisonMode="StrongWildcard" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00" maxReceivedMessageSize="20000000" maxBufferSize="20000000" maxBufferPoolSize="20000000" transferMode="Buffered" messageEncoding="Text" textEncoding="utf-8" bypassProxyOnLocal="false" useDefaultWebProxy="true" &gt; &lt;security mode="None" /&gt; &lt;/binding&gt; &lt;/basicHttpBinding&gt; &lt;/bindings&gt; &lt;/system.serviceModel&gt; &lt;/configuration&gt; </code></pre> <p><strong>IService.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace Phpwcfconsole { [ServiceContract] public interface IService { [OperationContract] string GetData(int value); } } </code></pre> <p><strong>Serverfunctions.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Phpwcfconsole { public partial class Service1 : IService { public string GetData(int value) { throw new Exception("error"); } } } </code></pre> <p><strong>ExeceptionHandler.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Dispatcher; using System.ServiceModel.Description; using System.Collections.ObjectModel; using System.ServiceModel.Configuration; namespace Phpwcfconsole { public class GlobalExceptionHandler : IErrorHandler { public bool HandleError(Exception ex) { return true; } public void ProvideFault(Exception ex, MessageVersion version, ref Message msg) { // Do some logging here var newEx = new FaultException( string.Format("CALLED FROM YOUR GLOBAL EXCEPTION HANDLER BY {0}", ex.TargetSite.Name)); MessageFault msgFault = newEx.CreateMessageFault(); msg = Message.CreateMessage(version, msgFault, newEx.Action); } } public class GlobalExceptionHandlerBehaviourAttribute : Attribute, IServiceBehavior { private readonly Type _errorHandlerType; public GlobalExceptionHandlerBehaviourAttribute(Type errorHandlerType) { _errorHandlerType = errorHandlerType; } public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase) { } public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection&lt;ServiceEndpoint&gt; endpoints, BindingParameterCollection parameters) { } public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) { var handler = (IErrorHandler)Activator.CreateInstance(_errorHandlerType); foreach (ChannelDispatcherBase dispatcherBase in serviceHostBase.ChannelDispatchers) { var channelDispatcher = dispatcherBase as ChannelDispatcher; if (channelDispatcher != null) channelDispatcher.ErrorHandlers.Add(handler); } } } } </code></pre> <p>ok so if i put some console.writeLine inside </p> <p><strong>public class GlobalExceptionHandlerBehaviourAttribute : Attribute, IServiceBehavior</strong> functions i see that they run when i start the program. but i cant get the example on the guide working with my function to throw a exception and get that one caught by my IErrorhandler.</p> <p>i've tried some exceptions inside other functions and nothing happens with my IErrorhandler.</p> <p>But one exception get caught in it that i discovered so far its when i add my service in <strong>Wcftestclient</strong> and then stop debug and remove <strong>[OperationContract]</strong> in my <strong>IService.cs</strong> and then start again and try to run the function without refreshing, that exception get caught by the IErrorhandler </p> <p>so my problem here is why cant i catch exceptions inside a function? thank you so much for answers. c(:</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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