tags:

views:

259

answers:

2

I am pretty new to WCF applications. I have a WCF application that is using NetTcpBinding. I wanted to invoke the functions in WCF service using the System.Reflection's Methodbase Invoke method. I mean I wanted to Dynamically call the Function by passing the String as the Function name. Reflection works great for Web Service or a Windows application or any dll or class. So their is certain way to do this for WCF also but I am not able to find that. I am getting the Assembly Name than it's type everything fine but as we cannot create an instance of the Interface class I tried to open the WCF connection using the binding and tried to pass that object but it's throwing the exception as :

"Object does not match target type."

I have opened the connection and passed the object and type is of interface only. I don't know whether I'm trying wrong thing or in wrong way. Any idea how shall I accomplish this??? The NetTCPBinding all are properly given while opening the connection. And one more thing I am using WCF as a Windows Service using NETTCPBinding.

A: 

The binding that I am using is NetTcpBinding as i said. The binding properties and WCF connection is as follows:

binding = new NetTcpBinding(SecurityMode.None);
binding.OpenTimeout = new TimeSpan(01, 12, 00);
binding.SendTimeout = new TimeSpan(01, 12, 00);
binding.ReceiveTimeout = new TimeSpan(01, 12, 00);
binding.CloseTimeout = new TimeSpan(01, 12, 00);
binding.TransferMode = TransferMode.Buffered;
binding.MaxReceivedMessageSize = 9000000;
binding.MaxConnections = 1000;
binding.ListenBacklog = 1000;   
ChannelFactory channel = new ChannelFactory<IclsInterfaceimp>();
EndpointAddress address = new EndpointAddress(strEndPoint);
channel.Endpoint.Binding = binding;
ObjInterface = channel.CreateChannel(address);
Type type = Type.GetType(typeName);//Getting the type of the interface.
MethodInfo method = type.GetMethod(methodName);//Getting the method here
return method.Invoke(ObjInterface, BindingFlags.Public | BindingFlags.InvokeMethod, null, parameters, System.Globalization.CultureInfo.CurrentCulture);//Getting Error here. Object does not match target type.

Wat shall i pass as an instance to the method.Invoke method?

Jankhana
+1  A: 

You are passing the correct instance when you invoke your method. This instance is the proxy object created via the interface-based call to ChannelFactory. I tried your technique on a hello world style application and got the expected results. One thing I don't see in your code example is how you initialize the parameters. That could be a problem. I believe think that your call to Type.GetType may be causing the error you are receiving. Notice I call GetType on the Proxy object. I include my sample code below that calls a Function GetData that takes one argument as an integer. ...

 Dim myFactory As ChannelFactory(Of SimpleService.IService1)
    myFactory = New ChannelFactory(Of SimpleService.IService1)(myBinding, myEndpoint)
    oProxy = myFactory.CreateChannel()
    'commented out version that does same call without reflection
    ' oProxy.GetData(3)
   Dim oType As Type = oProxy.GetType
   Dim oMeth As MethodInfo = oType.GetMethod("GetData")
   Dim params() As Object = {3}
   Dim sResults As String
   sResults = oMeth.Invoke(oProxy, BindingFlags.Public Or BindingFlags.InvokeMethod, Nothing, params, System.Globalization.CultureInfo.CurrentCulture)
John Wigger
Hey it worked!!! It was a silly mistake. I was again casting my type for the fully qualified interface instead of proxy. After pointing to my proxy it;s working now. Thanks alot.
Jankhana