2015-11-26 377 views
0

我想配置我的客戶端程序和我的聊天服務器程序的WCF端點。配置WCF端點?聊天服務器和客戶端

當我到那裏的邏輯嘗試連接(客戶端登錄到服務器)的時候,我得到以下錯誤:

Could not find endpoint element with name 'ChattingServiceEndpoint' and contract 'ChattingInterfaces.IChattingService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

這當我試圖打開在一個DuplexChannelFactory顯然發生在我的WPF聊天客戶端聊天窗口的後面代碼:

_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(), "ChattingServiceEndpoint"); 

我看到在客戶端和服務器的配置文件,爲「ChattingServiceEndpoint」的WCF配置。那麼真正缺少的是什麼?我把serviceModel標籤放在了app.config文件的錯誤部分嗎?我是否需要將它包含在我用來將客戶端連接到服務器的ChattingInterfaces庫的app.config中?

這裏是客戶端和服務器的app.config文件。

的app.config(服務器):

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <system.serviceModel> 
    <services> 
     <service name="ChattingServer.ChattingService"> 
     <endpoint address="net.tcp://localhost:9000/ChattingService" binding="netTcpBinding" bindingConfiguration="" name="ChattingServiceEndPoint" contract="ChattingInterfaces.IChattingService" /> 
     </service> 
    </services> 
    </system.serviceModel> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.8.7.0" newVersion="6.8.7.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <system.data> 
    <DbProviderFactories> 
     <remove invariant="MySql.Data.MySqlClient" /> 
     <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> 
    </DbProviderFactories> 
    </system.data> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="v11.0" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

的app.config(客戶端):

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="v11.0" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 

    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.8.7.0" newVersion="6.8.7.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
<connectionStrings><add name="otongadgethubEntities" connectionString="metadata=res://*/OTonGadgetHub.csdl|res://*/OTonGadgetHub.ssdl|res://*/OTonGadgetHub.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;password=abc123;persistsecurityinfo=True;database=otongadgethub&quot;" providerName="System.Data.EntityClient" /></connectionStrings> 
    <system.serviceModel> 
    <client> 
     <endpoint address="net.tcp://localhost:9000/ChattingService" binding="netTcpBinding" bindingConfiguration="" contract="ChattingInterfaces.IChattingService" name="ChattingServiceEndPoint" kind="" endpointConfiguration="" /> 
    </client> 
    </system.serviceModel> 
<system.data> 
    <DbProviderFactories> 
     <remove invariant="MySql.Data.MySqlClient" /> 
     <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> 
    </DbProviderFactories> 
    </system.data></configuration> 

下面是窗口中的代碼隱藏發生錯誤:

using ChattingInterfaces; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 

namespace ChatClient 
{ 
    /// <summary> 
    /// Interaction logic for ChatWPFClient.xaml 
    /// </summary> 
    public partial class ChatWPFClient : Window 
    { 
     //our current logged in user that will be set at MainWindow at login 
     public static user loggedInUser; 

     public static IChattingService Server; 
     private static DuplexChannelFactory<IChattingService> _channelFactory; 
     public ChatWPFClient() 
     { 
      InitializeComponent(); 
      _channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(), "ChattingServiceEndpoint"); 
      Server = _channelFactory.CreateChannel(); 

      int returnValue = Server.Login(loggedInUser.username); 
      if (returnValue == 1) 
      { 
       MessageBox.Show("You are already logged in, check to see who is logged into system"); 

      } 
      else if (returnValue == 0) 
      { 
       MessageBox.Show("You are now logged in! Welcome!"); 
       welcomeLabel.Text = "Welcome, " + loggedInUser.username; 

      } 
     } 

     private void sendMessage(object sender, RoutedEventArgs e) 
     { 
      //MessageBox.Show("Not available yet!"); 

      //TODO: Change username 
      if (chatEntryField.Text.Length == 0) 
      { 
       return; 
      } 
      Server.SendMessageToALL(chatEntryField.Text, "Test"); 
      TakeMessage(chatEntryField.Text, "Test"); 
      chatEntryField.Text = ""; 
     } 

     public void TakeMessage(string message, string userName) 
     { 
      chatBox.Text += userName + ": " + message + "\n"; 
      //TODO: Scroll chatBox to end 
     } 
    } 
} 

IChattingService.cs:

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

namespace ChattingInterfaces 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IChattingServices" in both code and config file together. 
    [ServiceContract(CallbackContract=typeof(IClient))] 
    public interface IChattingService 
    { 
     [OperationContract] 
     int Login(string userName); 
     [OperationContract] 
     void SendMessageToALL(string message, string userName); 
    } 
} 

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

我願意發佈更多代碼文件在必要時幫助診斷這一問題。任何人都知道該怎麼做/發生了什麼?謝謝!

回答

0

我又做了。

我發佈了我將歸類爲「poofus問題」的內容。原來,配置文件中的端點名稱和其他數據區分大小寫。確保端點完全匹配,在大小寫字符中一字不差!

+0

那麼請關閉你的問題,因爲這是一個簡單的錯字問題。 – Aron