所以我有一个客户端应用程序和一个 WCF 服务发布到 IIS 服务器。我成功地让客户端订阅 WCF 服务,然后在任何一个客户端上单击一个按钮后向客户端应用程序的每个实例推送一条消息。现在我的目标是从 WCF 应用程序中推送消息,但通过 Web 浏览器中的 REST,然后允许我在 Android 中使用此接口并在 Android 客户端和 Windows 客户端应用程序之间进行交互。有人知道我需要做什么吗?这是我工作的界面的基础知识。
IService.vb:
<ServiceContract(SessionMode:=SessionMode.Required, CallbackContract:=GetType(IServiceCallback))>
Public Interface IService
<OperationContract(IsOneWay:=True)>
Sub GetClientCount()
<OperationContract(IsOneWay:=True)>
Sub RegisterClient(ByVal id As Guid)
<OperationContract(IsOneWay:=True)>
Sub UnRegisterClient(ByVal id As Guid)
End Interface
Public Interface IServiceCallback
<OperationContract(IsOneWay:=True)>
Sub SendCount(ByVal count As Int32)
End Interface
服务.svc.vb
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Multiple)>
Public Class Service
Implements IService, IRestService
Private clients As New Dictionary(Of Client, IServiceCallback)()
Private locker As New Object()
Public ReadOnly Property Callback As IServiceCallback
Get
Return OperationContext.Current.GetCallbackChannel(Of IServiceCallback)()
End Get
End Property
'called by clients to get count
Public Sub GetClientCount() Implements IService.GetClientCount
Dim query = ( _
From c In clients _
Select c.Value).ToList()
Dim action As Action(Of IServiceCallback) = Function(Callback) GetCount(Callback)
query.ForEach(action)
End Sub
Private Function GetCount(ByVal callback As IServiceCallback) As Int32
callback.SendCount(clients.Count)
Return Nothing
End Function
'---add a newly connected client to the dictionary---
Public Sub RegisterClient(ByVal guid As Guid) Implements IService.RegisterClient
'---prevent multiple clients adding at the same time---
SyncLock locker
clients.Add(New Client With {.id = guid}, Callback)
End SyncLock
End Sub
'---unregister a client by removing its GUID from
' dictionary---
Public Sub UnRegisterClient(ByVal guid As Guid) Implements IService.UnRegisterClient
Dim query = From c In clients.Keys _
Where c.id = guid _
Select c
clients.Remove(query.First())
End Sub
End Class