2

Я следую вместе с учебником построить чат-клиент и сервер, прямо сейчас я получаю следующее сообщение об ошибке:C# ConcurrentDictionary использование несовместимой доступности?

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 

Вот файл класс, где соответствующий вопрос лежит (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); 
       } 
      } 
     } 
    } 
} 

Нужно ли объявлять тип в объекте ConcurrentDictionary в другом месте? В чем проблема? Благодаря!

+1

Я бы предположил, что ChattingServer.ConnectedClient не является общедоступным. – DaveShaw

ответ

4

ChattingService является общественным, а его _connectedClients участник является общественным. Но тип _connectedClients включает ChattingServer.ConnectedClient, который не является общедоступным.

Представьте, что вы должны были ссылаться на вашу ChattingServer сборки из другого проекта, а затем написать код так:

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

доступности вашего ChattingService типа и его _connectedClients поля позволяют получить доступ к этой области, но тип ConnectedClient не является общедоступным, поэтому у вас есть доступ к полю, тип которого должен быть скрыт, в соответствии с модификаторами доступности, которые вы разместили на типах и элементах в сборке ChattingServer. Вот почему вы получаете ошибку сборки: модификаторы доступности на ваших типах и элементах в сборке ChattingServer противоречат друг другу.

Если вы, например, сделаете ChattingServer.ConnectedClient общедоступным, или сделайте _connectedClients частным, это устранит проблему.

+0

* руки на лице * Вы, парни, абсолютно правы, я пропустил эту деталь. Сейчас работает отлично. Благодарю. –

Смежные вопросы