0

我有一个涉及使用自定义 IHttpModule 的 ASP.net 项目。此模块将位于管道中,当某些条件匹配时,它应该调用 WCF 服务上的方法,该服务托管在同一台机器上的简单 C# 控制台应用程序中。

该模块的代码如下:

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();
            }
        }        
    }
}

我的问题是这是第一次运行,但随后的尝试会导致此错误消息

通信对象 System.ServiceModel.Channels.ServiceChannel 不能用于通信,因为它处于故障状态。

好像我做错了什么,而且我一般是 WCF 的新手,所以这很有可能。

我认为问题在于重新创建 ChannelFactory,这会导致故障状态。

4

1 回答 1

1

具体错误可能意味着工厂出现故障,抛出异常(您正在吞下),然后当 finally 块执行时, factory.Close() 调用失败,因为工厂出现故障(如果 WCF 对象出现故障,您需要调用 Abort(),而不是 Close())。

于 2011-02-01T13:36:52.220 回答