开发者

Invoking WCF functions using Reflection

开发者 https://www.devze.com 2022-12-23 14:32 出处:网络
I have a WCF application that is using NetTcpBinding. I want to invoke the functions in WCF service using Methodbase.Invoke from the System.Reflection namespace. In other words I want to Dynamically c

I have a WCF application that is using NetTcpBinding. I want to invoke the functions in WCF service using Methodbase.Invoke from the System.Reflection namespace. In other words I want to Dynamically call a Function by passing a String as the Function name.

Reflection works great for Web Service or a Windows application or any DLL or class. So there is certainly a way to do this for WCF but I am unable to find out how.

I am getting the Assembly Name then 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 an 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 the wrong thing or am using the wrong way. Any idea how I can accomplish this?

The NetTCPBinding all are properly given while opening the connection. I am using WCF as a Windows Service using NETTCPBindi开发者_如何学Cng.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消