C# 如何处理ServiceMode il故障状态

C# 如何处理ServiceMode il故障状态,c#,asp.net,wcf,C#,Asp.net,Wcf,我按照此示例创建了ASP.NET网站和C项目之间的通信: 经过一段时间的不活动后,服务将进入故障状态,我无法再交换数据: 引发异常:mscorlib.dll中的“System.ServiceModel.CommunicationObjectFaultedException” 当我尝试从网站发送消息时,会引发此异常,如示例中所示: service.SendMessage("Hi, I'm the client"); 有没有办法使服务永久启用? 它将在局域网上工作,网页是一个控制界面,可能每隔几个

我按照此示例创建了ASP.NET网站和C项目之间的通信:

经过一段时间的不活动后,服务将进入故障状态,我无法再交换数据:

引发异常:mscorlib.dll中的“System.ServiceModel.CommunicationObjectFaultedException”

当我尝试从网站发送消息时,会引发此异常,如示例中所示:

service.SendMessage("Hi, I'm the client");
有没有办法使服务永久启用? 它将在局域网上工作,网页是一个控制界面,可能每隔几个小时发送一次数据

使现代化 此处显示服务器配置:

using System.ServiceModel;
using System;

namespace MyProject
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class IServer : Interfaces.IService
    {
        public void Connect()
        {
            Callback = OperationContext.Current.GetCallbackChannel<Interfaces.ICallbackService>();
        }

        public static Interfaces.ICallbackService Callback { get; set; }

        public void SendMessage(string message)
        {
            MessageReceivedEventArgs args = new MessageReceivedEventArgs();
            args.json = message;
            OnMessageReceived(this, args);
        }

        public event EventHandler<MessageReceivedEventArgs> MessageReceived;
        protected virtual void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            MessageReceived?.Invoke(this, e);
        }
    }

    public class MessageReceivedEventArgs : EventArgs
    {
        public string json;
    }
}

我最终得到了这个密码。它似乎有效,但我想知道它有多强大

public bool CheckConnection()
{
    if (pipeFactory == null)
    {
        pipeFactory = new DuplexChannelFactory<IService>(new InstanceContext(_instance), new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/ipc"));
    }

    try
    {
        switch (pipeFactory.State)
        {
            case CommunicationState.Faulted:
                pipeFactory.Abort();
                pipeFactory = new DuplexChannelFactory<IService>(new InstanceContext(_instance), new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/ipc"));
                service = pipeFactory.CreateChannel();
                service.Connect();
                break;

            case CommunicationState.Closed:
                service = pipeFactory.CreateChannel();
                service.Connect();
                break;

            case CommunicationState.Created:
                service = pipeFactory.CreateChannel();
                service.Connect();
                break;
        }

    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex);
        throw;
    }

    return pipeFactory.State == CommunicationState.Opened;
}
使现代化 这不管用。即使pipeFactory.State处于打开状态,在一段时间不活动后仍会引发以下异常:

引发异常:mscorlib.dll中的“System.ServiceModel.CommunicationObjectFaultedException”

System.ServiceModel.CommunicationObjects FaultedException:通信对象System.ServiceModel.Channel.ServiceChannel无法用于通信,因为它处于故障状态

服务器堆栈跟踪: 在System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen 在System.ServiceModel.Channels.ServiceChannel.CallString操作中,布尔单向,ProxyOperationRuntime操作,对象[]输入,对象[]输出,时间跨度超时 在System.ServiceModel.Channels.ServiceChannelProxy.InvokeServiceMethodCallMessage methodCall中,ProxyOperationRuntime操作 在System.ServiceModel.Channels.ServiceChannelProxy.InvokeMessage消息中

在[0]处重试异常: 在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessageIMessage reqMsg、IMessage retMsg 在System.Runtime.Remoting.proxy.RealProxy.PrivateInvokeMessageData和msgData中,Int32类型 在Interfaces.IService.SendMessageString消息中 在_Default.submit1默认值中,字符串名称、字符串姓氏、字符串国家/地区、字符串电子邮件、布尔免责声明1、布尔免责声明2。aspx.cs:第99行


由于您的服务只有一个实例,所以可以保持与它的永久连接。永久连接不好,因为它们会消耗资源,但如果您确实需要,则需要将两侧的receiveTimeout设置为TimeSpan.Max。这样,WCF将不会像现在这样关闭连接

在示例代码中,您需要在客户端和服务器代码中将新的NetNamedPipeBinding替换为新的NetNamedPipeBinding{ReceiveTimeout=TimeSpan.Max}


根据服务代码,您假设连接的客户端不超过1个,因为回调是静态字段,但仍要注意,如果客户端断开连接,则回调看起来是有效的,但如果您尝试发送它将抛出的内容。

为每个请求建立新连接?这正是我现在使用的解决方法!但我不认为这是一个好的做法…为什么不呢?在任何情况下,异常都会导致您的程序退出并重新启动某些部分。将其保持为无状态amap。能否显示服务器部件的配置?还有,服务的实例上下文模式是什么?@IgorLabutin:我已经更新了问题是的,我将只有一个客户端。请你再解释一下最后一句话好吗?我不确定是否理解正确。你是说当客户端断开连接,然后服务器试图通过回调发送数据吗?是的,没错。我看不出您现在在发布的代码示例中使用它。但若您确实使用了它,那个么请注意,若客户端断开连接,通过回调字段调用某些回调方法可能会抛出错误。很抱歉,我找不到任何ReceiveTimeout属性。也许我看错了课程。请问,哪个类具有此属性?如何配置端点?您可以使用config文件,也可以在C中使用。您是否可以显示客户端和服务器的代码/配置,然后我将告诉您要更新哪个属性。这是绑定的属性,因此您需要在配置文件中明确指定绑定,或者在代码中创建绑定时设置其属性。更新了我的回答,并解释了如何设置接收超时