【发布时间】: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