How to configure a reverse channel on a WCF service created through ChannelFactory?

I need my WCF service to raise events for clients. I read that this happens through the callback channel, and I implemented it as follows: Service interfaces:

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;
}

Service implementation:

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
}
}

Making a callback on the client:

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);
    }
}

But here is my problem: On the client side, I have to pass it to the service, for example:

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

according to this StackOverflow answer: stack overflow

But I don’t connect to my service like that, because my client does not know about the implementation of the service, he knows only two interfaces! Therefore, I connect as follows:

    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;
    }

So how do I make callbacks work?

UPDATE:

, , ChannelFactory DuplexChannelFactory BasicHTTPBinding WsDualHTTPBinding, . BasicHTTPBinding, . , :

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

^

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

^ .

, . , . 60 -, .

+4
1

, , . , . , , , , , . .

0

All Articles