Question

I have an ASP.net project which involves using a custom IHttpModule. This module will sit in the pipeline and when certain criteria match up, it should invoke a method on a WCF service hosted in a simple C# console application on the same machine.

The code for the module is below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.SessionState;
using System.Web;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration;
using System.ServiceModel;
using SimpleFarmStateServer;

namespace SimpleFarm
{
    public class SimpleFarmModuleSS : IHttpModule, IRequiresSessionState
    {
        protected string cache_directory = "";

        // WCF
        ChannelFactory<IStateServer> factory;
        IStateServer channel;

        public void Dispose() { }

        public void Init(System.Web.HttpApplication context)
        {            
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

            setupFactory();
        }

        void setupFactory()
        {
            factory = new ChannelFactory<IStateServer>(
                    new NetNamedPipeBinding(),
                    "net.pipe://localhost/StateServer");
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            try
            {
                if (factory.State != CommunicationState.Opened)
                    setupFactory();

                channel = factory.CreateChannel();
                channel.LogAccess("Hello World!");
            }
            catch (Exception ex)
            {

            }
            finally
            {
                factory.Close();
            }
        }        
    }
}

My problem is that this runs the first time, but then subsequent attempts cause this error message

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

It seems as if I am doing something wrong, and I am new to WCF in general so this is very likely.

I think the issue is surrounding the ChannelFactory being recreated, and this causes the faulted state.

Was it helpful?

Solution

The specific error probably means the factory faulted, threw an exception (which you're swallowing) and then when the finally block executes, the factory.Close() call fails because the factory is faulted (if a WCF object is faulted, you need to call Abort() on it, not Close()).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top