tags:

views:

56

answers:

1

I am trying to build a WCF service that exposes the functionality of a particular COM object that I do not have the original source for. I am using duplex binding so that each client has their own instance as there are events tied to each particular instance which are delivered through a callback (IAgent). It appears there is a deadlock or something because after the first action, my service blocks at my second action's lock. I have tried implementing these custom STA attribute and operation behaviors (http://devlicio.us/blogs/scott%5Fseely/archive/2009/07/17/calling-an-sta-com-object-from-a-wcf-operation.aspx) but my OperationContext.Current is always null. Any advice is much appreciated.

Service

Collection:

private static Dictionary<IAgent, COMAgent> agents = new Dictionary<IAgent, COMAgent>();

First action:

    public void Login(LoginRequest request)
    {
        IAgent agent = OperationContext.Current.GetCallbackChannel<IAgent>();
        lock (agents)
        {
            if (agents.ContainsKey(agent))
                throw new FaultException("You are already logged in.");
            else
            {
                ICOMClass startup = new ICOMClass();

                string server = ConfigurationManager.AppSettings["Server"];
                int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                bool success = startup.Logon(server, port, request.Username, request.Password);

                if (!success)
                    throw new FaultException<COMFault>(new COMFault { ErrorText = "Could not log in." });

                COMAgent comAgent = new COMAgent { Connection = startup };
                comAgent.SomeEvent += new EventHandler<COMEventArgs>(comAgent_COMEvent);
                agents.Add(agent, comAgent);
            }
        }
    }

Second Action:

    public void Logoff()
    {
        IAgent agent = OperationContext.Current.GetCallbackChannel<IAgent>();
        lock (agents)
        {
            COMAgent comAgent = agents[agent];
            try
            {
                bool success = comAgent.Connection.Logoff();

                if (!success)
                    throw new FaultException<COMFault>(new COMFault { ErrorText = "Could not log off." });

                agents.Remove(agent);
            }
            catch (Exception exc)
            {
                throw new FaultException(exc.Message);
            }
        }
    }
A: 

Take a look at this very similar post: http://www.netfxharmonics.com/2009/07/Accessing-WPF-Generated-Images-Via-WCF

You have to use an OperationContextScope to have access to the current OperationContext from the newly generated thread:

System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate
    {
        using (System.ServiceModel.OperationContextScope scope = new System.ServiceModel.OperationContextScope(context))
        {
            result = InnerOperationInvoker.Invoke(instance, inputs, out staOutputs);
        }
     }));
Jan