简体   繁体   English

使用REST公开WCF回调

[英]Expose WCF Callback with REST

So I have a client application and a WCF Service publish to an IIS server. 因此,我有一个客户端应用程序和一个WCF服务发布到IIS服务器。 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. 我成功地使客户端可以订阅WCF服务,然后在任意一个客户端上单击按钮后,将消息推送到客户端应用程序的每个实例。 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. 现在,我的目标是从WCF应用程序中推送消息,但通过Web浏览器中的REST推送消息,这将允许我在Android中使用此接口并在Android客户端和Windows客户端应用程序之间进行交互。 Anyone have any idea what i need to do? 有人知道我需要做什么吗? Here is the basics of the interface that I have working. 这是我正在使用的界面的基础。

IService.vb: 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 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. 通常,在HTTP中基于推的实现非常困难。 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). 用于实现此目的的通用技术是Reverse AjaxWeb套接字长轮询 (有些甚至使用单个像素的flex对象来保持套接字永久打开)。 Check out this Code Project Article , which uses Reverse Ajax aka Commet to implement a wcf rest service capable of push notification. 查阅此代码项目文章 ,该文章使用Reverse Ajax aka Commet来实现能够推送通知的wcf rest服务。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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