How to get an InstanceContext from a runtime proxy constructed from metadata of another service

I have the following function trying to create a callback InstanceContext from metadata of other services.

    private InstanceContext GetCallbackIC(Type proxy, ServiceEndpoint endpoint){
        try {
            IDuplexContextChannel dcc;
            PropertyInfo pi = proxy.GetProperty("InnerDuplexChannel");

            if (pi.GetIndexParameters().Length > 0) {
                dcc = (IDuplexContextChannel)pi.GetValue(Activator.CreateInstance(proxy, OperationContext.Current.InstanceContext, endpoint.Binding, endpoint.Address), new object[] { 0 });
            } else {
                dcc = (IDuplexContextChannel)pi.GetValue(Activator.CreateInstance(proxy, OperationContext.Current.InstanceContext, endpoint.Binding, endpoint.Address), null);
            return new InstanceContext(dcc.CallbackInstance);
        } catch (Exception ex) {
            return null;

"OperationContext.Current.InstanceContext" is not the right one here because it throws me an exception - "The InstanceContext provided to the ChannelFactory contains a UserObject that does not implement the CallbackContractType ..."

How to get the InstanceContext of the proxy?



I solved the problem by creating an implement object for the callback interface at run time after retrieved the metadata. InstanceContext is only a wrapper. My original thought is not right.

Need Your Help

Using LINQ Group By to return new XElements

c# linq .net-3.5 c#-3.0 linq-to-xml

I have the following code and got myself confused: