[英]Expose WCF Callback with REST
因此,我有一個客戶端應用程序和一個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
Service.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
通常,在HTTP中基於推的實現非常困難。 用於實現此目的的通用技術是Reverse Ajax , Web套接字或長輪詢 (有些甚至使用單個像素的flex對象來保持套接字永久打開)。 查閱此代碼項目文章 ,該文章使用Reverse Ajax aka Commet來實現能夠推送通知的wcf rest服務。
您需要將消息從Android設備發送到wcf服務,然后wcf服務會將消息轉發到注冊的Windows客戶端。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.