简体   繁体   中英

Expose WCF Callback with REST

So I have a client application and a WCF Service publish to an IIS server. I successfully was able to get the clients to subscribe to the WCF service and then push out a message to each of the instances of the client app after a button was clicked on any 1 of the clients. Now my goal is to push out the message from the WCF application but through REST in a web browser which would then allow me to use this interface in Android and interact between an Android client and the windows client application. Anyone have any idea what i need to do? Here is the basics of the interface that I have working.

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

Generally push based implementation in HTTP is pretty difficult. Commom techniques used to implement this is either Reverse Ajax , Web Sockets or Long Polling (Some even use a single pixel flex object to keep the socket permanently open). Check out this Code Project Article , which uses Reverse Ajax aka Commet to implement a wcf rest service capable of push notification.

您需要将消息从Android设备发送到wcf服务,然后wcf服务会将消息转发到注册的Windows客户端。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM