簡體   English   中英

如何在通過ChannelFactory創建的WCF服務上設置回調通道?

[英]How to set up callback channel on WCF service created via ChannelFactory?

我需要我的WCF服務來向客戶端發送事件。 我已經讀過通過回調通道發生的事情,我已經按照以下方式實現了它:服務接口:

public interface IServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void OnNewAlert(Alert a);
    [OperationContract(IsOneWay = true)]
    void OnProductEdited(Product p);
    [OperationContract(IsOneWay = true)]
    void OnHighlightChanged(Dictionary<User, List<Product>> highlighted);
    [OperationContract(IsOneWay = true)]
    void OnCatalogUpdated();


    event EventHandler NewAlert;
    event EventHandler ProductEdited;
    event EventHandler HighlightChanged;
    event EventHandler CatalogUpdated;
}
[ServiceContract(CallbackContract = typeof(IServiceCallback))]
public interface IService : IDisposable
{
    [OperationContract]
    List<Product> GetProducts(Predicate<Product> match = null, int limit = 0, string username = null);
    [OperationContract]
    Product GetProduct(Predicate<Product> match, string username = null);
    [OperationContract]
    Product GetRandomProduct(Predicate<Product> match = null, string username = null);
    [OperationContract]
    int GetFlagIndex(string flagName);
    [OperationContract]
    void SetFlag(string pid, string flagName, bool value);
    [OperationContract]
    List<Alert> GetAlerts(string username);
    [OperationContract]
    void DismissAlert(Alert alert, String username);
    [OperationContract]
    void HighlightProduct(List<string> pids, string user);
    [OperationContract]
    void EditProduct(string pid, Dictionary<string, object> fieldValues, string username = null);
    [OperationContract]
    void AttachModule(IModule m);
    [OperationContract]
    void Ping();

    event EventHandler NewAlert;
    event EventHandler ProductEdited;
    event EventHandler HighlightChanged;
    event EventHandler CatalogUpdated;
}

服務實施:

namespace Service
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class ServiceInstance : IService
{
    List<IServiceCallback> callbackChannels = new List<IServiceCallback>();
    //other vars

    public ServiceInstance()
    {
            //lots of stuff here
    }

    private User SignalUser(string username)
    {
        if (username == null)
            return null;

        IServiceCallback channel = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
        if (!callbackChannels.Contains(channel)) //if CallbackChannels not contain current one.
        {
            callbackChannels.Add(channel);
        }

        User user = knownUsers.Find(p => p.username == username);
        if (user == null)
        {
            user = new User();
            user.username = username;
            user.highlighColor = Color.FromArgb(r.Next(0, 128), r.Next(0, 128), r.Next(0, 128));
            knownUsers.Add(user);
            foreach (KeyValuePair<Alert, List<User>> kvp in alerts)
            {
                kvp.Value.Add(user);
            }
        }
        user.lastOnline = DateTime.Now;
        if(!onlineUsers.Contains(user))
            onlineUsers.Add(user);

        return user;
    }

    //lots of other things here
}
}

客戶端的回調實現:

class ServiceEventHandler : IServiceCallback
{
    public event EventHandler NewAlert;
    public event EventHandler ProductEdited;
    public event EventHandler HighlightChanged;
    public event EventHandler CatalogUpdated;

    public void OnCatalogUpdated()
    {
        CatalogUpdated?.BeginInvoke(null, null, null, null);
    }

    public void OnHighlightChanged(Dictionary<User, List<Product>> highlighted)
    {
        HighlightChanged?.BeginInvoke(highlighted, EventArgs.Empty, null, null);
    }

    public void OnNewAlert(Alert a)
    {
        NewAlert?.BeginInvoke(a, EventArgs.Empty, null, null);
    }

    public void OnProductEdited(Product p)
    {
        ProductEdited?.BeginInvoke(p, EventArgs.Empty, null, null);
    }
}

但這是我的問題:在客戶端,我應該將它傳遞給這樣的服務:

EventHandler eventHandler = new EventHandler();
MyServiceClient client = new MyServiceClient(new InstanceContext(eventHandler));

根據StackOverflow的回答: https ://stackoverflow.com/a/1143777/2018696

但我沒有像這樣連接到我的服務,因為我的客戶端不知道服務的實現,它只知道這兩個接口! 所以我這樣連接:

    public static IService GetService(string serviceAddress)
    {
        Uri service_uri = new Uri(serviceAddress);
        var endpoint = new EndpointAddress(service_uri, new[] { AddressHeader.CreateAddressHeader(settings["username"], "", "") });
        IService service = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), endpoint);
        return service;
    }

那么如何讓回調起作用呢?

更新:

好吧,正如評論所提出的,我用DuplexChannelFactory替換了ChannelFactory,用WsDualHTTPBinding替換了BasicHTTPBinding,我沒有得到服務器的響應。 如果我刮開回調處理程序,我會得到BasicHTTPBinding的響應。 基本上:

[ServiceContract]
BasicHttpBinding();
ChannelFactory<IService>.CreateChannel(binding, endpoint);

^這有效

[ServiceContract(CallbackContract = typeof(IServiceCallback))]
WSDualHttpBinding(WSDualHttpSecurityMode.None);
DuplexChannelFactory<IService>.CreateChannel(new InstanceContext(handler), binding, endpoint);

^這不。

它適用於localhost,但不適用於LAN或Internet。 服務器和客戶端都禁用防火牆。 當我嘗試聯系服務器時,我得到60秒超時。

我發現我的連接問題是由於在客戶端的路徑上沒有設置端口轉發。 由於我無法確保對客戶端進行正確的端口訪問,因此我已恢復為非回調模型,並將使用來自客戶端的常規請求從服務接收累積的事件數據。 可能效率不高,但到目前為止似乎是一種萬無一失的方法。 謝謝大家的關注。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM