【问题标题】:Sending table from Server to Client via [OperationContract]通过 [OperationContract] 从服务器发送表到客户端
【发布时间】:2014-12-18 09:35:09
【问题描述】:

我有一些问题。我有一个关于 WCF 技术的简单聊天。聊天程序由服务器部分和客户端部分组成。我需要从数据库中打包一个表并将其发送给客户端。但是我不知道如何正确地做到这一点,所以我决定这样做,当然,我有一个问题:客户没有收到桌子。会是什么?

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

    namespace ChattingInterfaces ///chatting interface
    {
        [ServiceContract(CallbackContract=typeof(IClient))]
        public interface IChattingService
        { 
            [OperationContract]
            List<ITableContent> content();
        }
    }
    ////////////////////////////////////////

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

namespace ChattingServer /// server part of program
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
    public class ChattingService : IChattingService
    {
        public List<ITableContent> content()
        {
            List<ITableContent> result = new List<ITableContent>();
            result.Add(new TableContent()); /// packing table to client
            return result;
        }

    }
}
////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ChattingInterfaces
{
    public interface ITableContent
    {
        List<string> nick { get; set; }
        List<bool> status { get; set; }
        List<decimal> id { get; set; }
    }
}
////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ChattingInterfaces;
using DataContext1;
using Devart.Data.Oracle;
using System.Data;

namespace ChattingServer
{
    public class TableContent:ITableContent
    {
        ABDDataContext db;
        List<object> rList;

        List<string> ITableContent.nick
        {
            get
            {
                return db.TABLEs.AsParallel().Select(f => f.NICK).ToList();
            }
            set
            {  
            }
        }

        List<bool> ITableContent.status
        {
            get
            {
                return db.TABLEs.AsParallel().Select(f => f.STATUS).ToList();
            }
            set
            {
            }
        }

        List<decimal> ITableContent.id
        {
            get
            {
                return db.TABLEs.AsParallel().Select(f => f.ID).ToList();
            }
            set
            {
            }
        }
    }
}
////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using ChattingInterfaces;
using System.ServiceModel;

namespace ChattingClient
{
    public partial class MainWindow : Window
    {
        public static IChattingService Server;
        private static DuplexChannelFactory<IChattingService> _channelFactory;

public MainWindow()
{
    InitializeComponent();
    _channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallBack(this),"ChattingServiceEndPoint");
    Server = _channelFactory.CreateChannel();
    var p = Server.content(); /// CommunicationException was unhandled!
}

////////////////////////////////////////

【问题讨论】:

  • 尝试使用signalr。它是创建聊天的简单方法

标签: c# .net wcf networking


【解决方案1】:

您有关于 knowntypes 的问题,在 WCF 中发送接口是非常有问题的 - 因为您需要创建 knowntype 解析器。而聊天应用使用 WCF 就像用 1 吨炸弹击中钉子一样。为什么不使用自托管或非自托管的 WebAPI 服务器,而使用 Signalr 客户端?

无论如何,您的表格内容没有 datacontract 属性 - WCF 不知道要序列化什么

【讨论】:

  • 我需要 WCF,因为聊天只是整个系统的一部分。如果你能告诉我发送表格的正确方法(这是唯一的目标),我将非常感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
相关资源
最近更新 更多