It is sometimes very important to let the server knows when the client has made a proxy which may talks to the server.
However due to the complication and the design that WCF server has employeed to separate the low-level connection based management from the actual proxy itself. It is really hard to tell when new client has been attempted on the server.
You may try something Get the Dispatcher associated with a ServiceHost and then try to get the Listener (IChannelListener which is attached to the Dispatcher)... Or even, you can create/Build the IChannelListener and then you can simulate the server in the Berkely style like socket api. You can check on the reference "How do I create a simple Web Server using WCF without the ServiceHost class?How do I create a simple Web Server using WCF without the ServiceHost class?" Doing this is OK if you are interactiing with system which interact with each other via soap/xml messages. However, you lost the power of encapsulate of the ServiceHost, which has the ability to dispatch the call to the right instance calll.
However, the WCF system is quite flexible, you can even simuate your own connecion based management interace.
So I will make the following interface.
[ServiceContract]
public interface IConnectionService
{
[OperationContract(IsOneWay = true)]
void Connect();
[OperationContract(IsOneWay = true)]
void Disconnect();
}
and we shall make our service inerface inherited interface of this Connection Serivce.
[ServiceContract(CallbackContract = typeof(IPushCallback))]
public interface IPushService : IHeartbeatService, IConnectionService
{
[OperationContract(IsOneWay = true)]
void Subscribe(string tableId);
[OperationContract(IsOneWay = true)]
void UnSubscribe(string tableId);
[OperationContract(IsOneWay = false)]
bool IsSubscribed(string tableId);
[OperationContract(IsOneWay = false)]
string[] GetTableIds();
}
Then the client should do the following for an valid connection.
InstanceContext instanceContext = new InstanceContext(new PushCallback());
ServiceEndpoint endpoint = new ServiceEndpoint(
ContractDescription.GetContract(typeof(IService)),
new NetTcpBinding(),
new EndpointAddress("net.tcp://127.0.0.1:9999/PushService"));
//using (DuplexChannelFactory<IPushService> channelFactory = new DuplexChannelFactory<IPushService>(instanceContext, "PushService"))
using (DuplexChannelFactory<IPushService> channelFactory = new DuplexChannelFactory<IPushService>(instanceContext, endpoint))
{
IPushService proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
proxy.Connect();
string[] tableNames = proxy.GetTableIds();
Console.WriteLine("Available Table Ids: \n{0}", string.Join("\n", tableNames));
proxy.Subscribe(tableNames[0]);
proxy.Subscribe(tableNames[1]);
proxy.Subscribe(tableNames[2]);
proxy.Subscribe(tableNames[3]);
proxy.Subscribe(tableNames[4]);
//proxy.Subscribe(tableNames[5]); // Union has problem
//proxy.Subscribe(tableNames[6]); // Source3 has problem
Console.WriteLine("the table {0} {1} registered", tableNames[0], proxy.IsSubscribed(tableNames[0]) ? "is" : "is not");
Console.WriteLine("the table {0} {1} registered", "NonExistingTableId", proxy.IsSubscribed("NonExistingTableId") ? "is" : "is not");
proxy.UnSubscribe(tableNames[0]);
Console.ReadLine();
proxy.Disconnect();
Console.WriteLine("Press any key to exit!");
Console.Read();
}
}
And in the implemetatin of the Service.
And as the implementor, you will need to maintain the inner state of the server, and you should check that the client is connected first.
e.g.
public string[] GetTableIds()
{
TabularChannelManager.Instance.EnsureConnected(OperationContext.Current.GetCallbackChannel<ITabularPushCallback>());
return TableIdDictWrapper.Instance.TableIdDict.Keys.ToArray<string>();
}
and the EnsureConnected call is something like this:
internal void EnsureConnected(IPushCallback callbackChannel)
{
if (callbackChannel == null) throw new ArgumentNullException("callbackchannel");
lock (SyncObj)
{
if (!clients.Contains(callbackChannel))
throw new InvalidOperationException("Client is not connected");
}
}
References
:Notify Server if Client connecs.
How do I create a simple Web Server using WCF without the ServiceHost class?

本文介绍了一种在WCF中实现客户端连接管理的方法,通过定义IConnectionService接口并结合DuplexChannelFactory创建双工通道来通知服务器客户端的连接状态。
1399

被折叠的 条评论
为什么被折叠?



