我的客户端的双工WCF net.tcp服务处置对象异常

时间:2016-01-19 13:18:20

标签: c# wcf client-server duplex duplex-channel

我实现了一个客户端 - 服务器应用程序,用于与WCF聊天。我有一个广播回调。它正常工作,直到我改变了一些功能。我正在使用我的数据集修复数据。它在我的服务器上初始化并被使用。我实现了一个XML解析器机制,然后我得到了这样的错误:

  

无法访问已处置的对象。对象名称:   ' System.ServiceModel.InstanceContext'

我正在使用我的服务:

System.ServiceModel.InstanceContext context = new System.ServiceModel.InstanceContext(this.cb);
this._client = new WCFDevComServiceClient(context);

然后我打开this._client只有一次,我在每个电话中都使用这个对象。 (我不会随时关闭这个对象。)

如果我在每次通话之前打开并关闭它,那么它正在工作。但在这种情况下,我的回调函数不起作用。而我无法播出。所以我无法关闭我的客户端对象。

您是否有关于此错误的更多信息?或任何修复建议?

我的服务代码:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WCFDevComService : IWCFDevComService
{
    private const string deviceXmlPath = @"D:\MyDocuments\Visual Studio 2015\Projects\WCFBroadcastorService\WCFDeviceCom\WCFDeviceCom_Service\bin\Devices.xml";
    private const string userXmlPath = @"D:\MyDocuments\Visual Studio 2015\Projects\WCFBroadcastorService\WCFDeviceCom\WCFDeviceCom_Service\bin\Users.xml";
    private DbOperations.DbOperations dbOperator = new DbOperations.DbOperations();
    private static List<User> UserCollection = new List<User>(); 
    private static Dictionary<string, IWCFDevComServiceBroadcastorCallback> clients = new Dictionary<string, IWCFDevComServiceBroadcastorCallback>();
    private static object locker = new object(); 
    private List<Device> deviceCollection = new List<Device>();

    public bool LoginToServer(string clientName)
    {
        bool retVal = false;

        if (clientName != "")
        { 

            retVal = CheckUserName(clientName);
            if (true == retVal)
            {
                try
                {
                    IWCFDevComServiceBroadcastorCallback callback =
                        OperationContext.Current.GetCallbackChannel<IWCFDevComServiceBroadcastorCallback>();

                    if (clients.Keys.Contains(clientName))
                        clients.Remove(clientName);
                    clients.Add(clientName, callback);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        return retVal;
    }

    private bool CheckUserName(string clientName)
    {
        lock (locker)
        {
            UserCollection = dbOperator.QueryForGetAllUsers(userXmlPath);

            foreach (var user in UserCollection)
            {
                if (user.m_userName.Contains(clientName))
                {
                    return true;
                }
            }

            return false;
        }
    }

    public List<Device> GetAllDevicesInformation()
    {
        try
        {
            return GetInformationFromDeviceXML();

        }
        catch (Exception ex)
        {
            throw;
            return null;
        }

    }

    private List<Device> GetInformationFromDeviceXML()
    {
        try
        {
            lock (locker)
            {
                deviceCollection = dbOperator.QueryForGetAllDevices(deviceXmlPath);
            }

            return deviceCollection;
        }
        catch (Exception ex)
        {
            throw;
            return null;
        }

    }

    public int GetNumberOfDevices()
    {
        try
        {
            lock (locker)
            {
                //todo: GetNumberOfDevicesFromXML 
                return dbOperator.QueryForGetDeviceCount(deviceXmlPath);
            }
        }
        catch (Exception ex)
        {
            throw;
            return 0;
        }

    }

    public Device GetDeviceInformation(int index)
    {
        List<Device> deviceCollection = GetInformationFromDeviceXML();

        return deviceCollection[index];
    }

    public int BlockedDevice(int index, User user)
    {
        try
        {
            if (index < 0 && user == null)
                return -1; //fail

            lock (locker)
            {
                if (deviceCollection[index].m_state.m_state != States.Free)
                    return 1; // isn't free for blocking
                DeviceState tmpState = new DeviceState();
                tmpState.m_state = States.Locked;
                tmpState.m_lockedUser = user;
                deviceCollection[index].m_state = tmpState;
                //todo: UpdateXML
                dbOperator.QueryForUpdateDeviceStateById(deviceXmlPath, index + 1, tmpState);
                //deviceCollection = GetInformationFromDeviceXML();
                NotifyOtherClients(index, user);
                return 0; //success
            }
        }
        catch (Exception ex)
        {
            throw;
            return -1;

        }


    }

    public int FreeDevice(int index, User user)
    {
        try
        {
            if (index < 0 && user == null)
                return -1; //fail

            lock (locker)
            {
                if (deviceCollection[index].m_state.m_state != States.Locked)
                    return 1; // isn't locked to free

                if (deviceCollection[index].m_state.m_lockedUser.m_userName != user.m_userName && user.m_userPermission != Permission.ADMIN)
                    return 2; // isn't right person
                DeviceState tmpState = new DeviceState();
                tmpState.m_state = States.Free;
                tmpState.m_lockedUser = null;
                deviceCollection[index].m_state = tmpState;
                //todo: UpdateXML
                dbOperator.QueryForUpdateDeviceStateById(deviceXmlPath, index + 1, tmpState);
                //deviceCollection = GetInformationFromDeviceXML();
                NotifyOtherClients(index, user);
                return 0; //success
            }
        }
        catch (Exception ex)
        {
            throw;
            return -1;
        }


    }

    public Permission GetPermission(string userName)
    {
        lock (locker)
        {
            foreach (var user in UserCollection)
            {
                if (user.m_userName == userName)
                    return user.m_userPermission;
            }

            return Permission.USER;
        }
    }


    private void NotifyOtherClients(int index, User user)
    {
        try
        {
            var inactiveClients = new List<string>();
            EventDataType eventData = new EventDataType();
            eventData.ClientName = user.m_userName;
            eventData.Index = index;
            foreach (var client in clients)
            {
                if (client.Key != eventData.ClientName)
                {
                    try
                    {
                        client.Value.BroadcastToClient(eventData);
                    }
                    catch (Exception ex)
                    {
                        inactiveClients.Add(client.Key);
                    }
                }
            }

            if (inactiveClients.Count > 0)
            {
                foreach (var client in inactiveClients)
                {
                    clients.Remove(client);
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        } 
    }
}

0 个答案:

没有答案