3

I'm following along with a tutorial to build a chat client and server, right now I've got the following error:

Inconsistent accessibility: field type 'System.Collections.Concurrent.ConcurrentDictionary<string,ChattingServer.ConnectedClient>' is less accessible than field 'ChattingServer.ChattingService._connectedClients' c:\Users\KOEMXE\Documents\Visual Studio 2012\Projects\ChatApplication\ChattingServer\ChattingService.cs 17  62  ChattingServer

Here's the class file where the relevant issue lies (ChattingService.cs):

using ChattingInterfaces;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace ChattingServer
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
    public class ChattingService : IChattingService
    {
        //private ConnectedClient _connectedClients;

        public ConcurrentDictionary<string, ConnectedClient> _connectedClients = new ConcurrentDictionary<string, ConnectedClient>();

        public int Login(string userName)
        {
            //is anyone else logged in with my name?
            foreach (var client in _connectedClients)
            {
                if(client.Key.ToLower() == userName.ToLower())
                {
                    //if yes
                    return 1;
                }
            }

            var establishedUserConnection = OperationContext.Current.GetCallbackChannel<IClient>();

            ConnectedClient newClient = new ConnectedClient();
            newClient.connection = establishedUserConnection;
            newClient.UserName = userName;



            _connectedClients.TryAdd(userName, newClient);

            return 0;
        }


        public void SendMessageToALL(string message, string userName)
        {
            foreach (var client in _connectedClients)
            {
                if (client.Key.ToLower() != userName.ToLower())
                {
                    client.Value.connection.GetMessage(message, userName);
                }
            }
        }
    }
}

Do I need to declare the type in the ConcurrentDictionary object elsewhere? What's the issue? Thanks!

Kieran Ojakangas
  • 495
  • 4
  • 18

1 Answers1

5

ChattingService is public, and its _connectedClients member is public. But the type of _connectedClients involves ChattingServer.ConnectedClient, which is not public.

Imagine you were to reference your ChattingServer assembly from another project and then write code like this:

using ChattingServer;
...
ConcurrentDictionary<string, ConnectedClient> dict = myChattingService._connectedClients;

The accessibility of your ChattingService type and its _connectedClients field allow you to access that field, but the type ConnectedClient isn't public, so you have access to a field whose type should be hidden, according to the accessibility modifiers you have placed on the types and members in the ChattingServer assembly. This is why you are getting the build error: the accessibility modifiers on your types and members in the ChattingServer assembly contradict each other.

If you, for example, make ChattingServer.ConnectedClient public, or make _connectedClients private, that will fix the problem.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173