1

I'm using the Entity Framework to create a ticketing system for IT help requests. The client app simply inserts a new row into a table called Requests with description of the request etc. I want the ticket manager application to be able to read these from the database, and be updated when new requests are inserted into the database.

I've followed an example here, and modified the code a bit: SqlDependency with EntityFramework 6 (async)

        public MainWindow()
    {
        InitializeComponent();

        var data1 = WaitForRequestList();

        Task.WaitAll(data1); // This never finishes

        List<Request> list = data1.Result;

        MessageBox.Show(list.ToString()); // So this never pops up
    }

    async Task<List<Request>> WaitForRequestList()
    {
        using (HelpDeskTicketingEntities context = new HelpDeskTicketingEntities()) // subclass of DbContext
        {
            SqlDependency.Start(context.Database.Connection.ConnectionString);
            SqlDependency dependency = new SqlDependency();
            dependency.OnChange += (sender, e) =>
            {
                Console.Write(e.ToString()); // This prints once, but does not on subsequent inserts
            };

            Task<List<Request>> task = Task<Task<List<Request>>>.Factory.StartNew(async () =>
            {
                System.Runtime.Remoting.Messaging.CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
                return await context.Requests.Where(r => r.status == "Open").ToListAsync();
            }).Unwrap();

            return await task;
        } 
    }

But the issue I'm seeing is that the list of requests is never built. ToListAsync() never finishes.

Anyone know what's wrong?

Additionally, I'd like to have the SqlDependancy always watching for changes, rather than it only working one time. However, I think I can figure it out if I can solve my previous problem.

Community
  • 1
  • 1

0 Answers0