问题
I successfully created a service moniker as client for my WCF service. But I'm unable to call any method on the moniker.
At the WCF service end I have a dummy method named TestMethod, as follows:
Public Function TestMethod(ByVal TestValue As String) As String Implements ICustomerService.TestMethod
Return "You said.... " & TestValue
End Function
Following code creates the Moniker in Excel.
Public Sub WCFMexMonkierDemo()
' Create a string for the service moniker including the content of the WSDL contract file
Dim mexMonikerString As String
mexMonikerString = "service:mexAddress='http://localhost/CustomerService.svc/mex'" & _
", address='http://localhost/CustomerService.svc'" & _
", binding=CustomerServices.CustomerService" & _
", bindingNamespace='http://tempuri.org/'" & _
", contract=ICustomerService" & _
", contractNamespace='http://tempuri.org/'"
' Create the service moniker object
Dim mexMoniker, result
Set mexMoniker = GetObject(mexMonikerString)
result = mexMoniker.TestMethod("client call") '<-- error on this line
'Set result = mexMoniker.TestMethod("client call")
MsgBox result
Set mexMoniker = Nothing
Set result = Nothing
End Sub
The above code works upto the GetObject
call, which implies that the moniker is successfully created. But I get an error as soon as I try to call any Method on it.
The WCF method works perfectly ok with Microsoft WCF Test Client, and other WCF clients. So I know there is no problem with the service itself.
回答1:
For anyone facing the same problem, here is the solution. I found out the cause of problem myself by a bit of researching, and below is what I did to get around it.
Cause of Problem
(It seems like) programs that use COM interfaces using a moniker string to connect to WCF services have problems with complex types (like classes, structures, arrays etc.). They can only work with simple types only (like string, integer, decimal, boolean etc.). Complex types in either method arguments or return type does not work.
Even if the method you want to call may not have any complex types at all in their method arguments or return type, they will not work if there is at-least one method in the service that has.
In my case, the methods that I was interested in did not have any complex types as method arguments or return types.
My Solution
Once I found out what was causing the problems, finding a solution around it was easy. I just created a separate WCF service (interface) for the methods of my interest and ensured that there are no complex types publicly exposed, i.e. no complex types in method definition - method arguments and return types.
Next I created a class that implements this interface, as with any other WCF service. I derived this class from the original one, so that I don't need to repeat all the business logic implemented there. The only purpose this class solves is to get past the limitation I was facing. It was just a wrapper around the original class with limited number of methods in it. The methods simply called the base class equivalent method and returned the resulting output directly to the client.
Then I modified my app.config
file to host this new service, and then replaced the service address in moniker string with this new service address. So, basically I ended up hosting two WCF services - one for the clients that can consume complex types and the other for those that can't.
That's all that was required, and everything is working perfectly OK now. :)
NOTE that this is just based on my own observation, and I could be wrong. There may be something I might be missing, and there could be better ways to get around this problem. If you find that this is the case, feel free to comment or post your solution.
来源:https://stackoverflow.com/questions/25744843/automation-error-when-invoking-method-on-wcf-mex-moniker-with-excel