Click here to Skip to main content
15,905,616 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi,
I calling a WCF Service using reflections . Where I'm creating a Proxy by using ChannelFactory ,binding and endpoint address.
Let us think that My ServiceContract Name is ISeravice1.
I'm creating a Proxy and calling a specific method in this way:
using (ChannelFactory<T> cf = new ChannelFactory<T>(binding, uri))
            {
                object oProxy = cf.CreateChannel();
                Type oType = oProxy.GetType();
                MethodInfo oMeth = oType.GetMethod(method);
                oMeth.Invoke(oProxy, args);
            }

In the above code I need to pass <T> as my ServiceContact that is IService1.
If I have many Service Contracts then I need to specify all , so I need to remove hardcoding of name of Service Contract.
Regards,
Ramana
Posted
Updated 24-May-11 1:03am
v2
Comments
Sergey Alexandrovich Kryukov 24-May-11 15:56pm    
Good question! Hard-coding the name is bad. My 5.
--SA

1 solution

Since you're using reflection anyways, you could instantiate the generic ChannelFactory class using reflection.

C#
public static object CreateGeneric(Type generic, Type innerType, params object[] args)
{
    System.Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
    return Activator.CreateInstance(specificType, args);
}

var contract = typeof(ICoolWebService); // or whatever
var cf = CreateGeneric(typeof(ChannelFactory), contract);



Credit[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 15:57pm    
Good, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900