配置WCF端点?聊天服务器和客户端

时间:2015-11-26 00:00:41

标签: c# wpf wcf .net-4.5 nettcpbinding

我正在尝试配置客户端程序和聊天服务器程序的WCF端点。

当我到达逻辑尝试连接的点(客户端登录到服务器)时,我收到以下错误:

  

找不到名为“ChattingServiceEndpoint”的端点元素   并在ServiceModel中签订'ChattingInterfaces.IChattingService'合同   客户端配置部分。这可能是因为没有配置   找到了您的应用程序的文件,或者因为没有端点元素   匹配此名称可以在客户端元素中找到。

当我尝试在WPF聊天客户端的聊天窗口后面的代码中打开DuplexChannelFactory时,显然会发生这种情况:

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

我愿意根据需要发布更多代码文件来帮助诊断此问题。任何人都知道该怎么做/正在发生什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我又做了。

我发布了我将其归类为“poofus问题”的内容。原来,配置文件中的端点名称和其他数据区分大小写。确保端点在大小写字母中完全匹配!