自动化在使用Excel调用WCF mex Moniker方法时出现错误

我成功地创build了一个服务名字对象作为我的WCF服务的客户端。 但是我无法用绰号来调用任何方法。

在WCF服务端,我有一个名为TestMethod的虚拟方法,如下所示:

Public Function TestMethod(ByVal TestValue As String) As String Implements ICustomerService.TestMethod Return "You said.... " & TestValue End Function 

以下代码在Excel中创buildMoniker。

 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 

上面的代码工作到GetObject调用,这意味着成功创build了名字对象。 但是,只要我尝试调用任何方法,就会收到错误。

自动化错误

WCF方法与Microsoft WCF Test Client以及其他WCF客户端完美兼容。 所以我知道服务本身没有问题。

对于任何面临同样问题的人,这里是解决scheme。 我通过一些研究发现了问题的原因,下面是我做的。

问题的原因

(看起来像)程序使用COM接口使用monikerstring连接到WCF服务有复杂types(如类,结构,数组等)的问题。 他们只能使用简单的types(如string,整数,小数,布尔等)。 方法参数或返回types中的复杂types不起作用。

即使您要调用的方法在其方法参数或返回types中可能根本没有任何复杂的types,但是如果服务中至less有一个方法,则它们将不起作用。

在我的情况下,我感兴趣的方法没有任何复杂的types作为方法参数或返回types。

我的解决scheme

一旦发现问题的原因,find一个解决scheme很容易。 我为我感兴趣的方法创build了一个单独的WCF服务(接口),并确保没有公开的复杂types,即方法定义中没有复杂的types – 方法参数和返回types。

接下来,我创build了一个实现此接口的类,就像其他任何WCF服务一样。 我从原来的这个类派生出来的,所以我不需要重复那里实现的所有业务逻辑。 这个课程解决的唯一目的是克服我面临的限制。 它只是一个包装原始类的方法,其中的方法数量有限。 这些方法简单地称为基类等价方法,并将结果输出直接返回给客户端。

然后,我修改了我的app.config文件来承载这个新的服务,然后用这个新的服务地址replace了monikerstring中的服务地址。 所以,基本上我最终托pipe了两个WCF服务 – 一个用于可以使用复杂types的客户端,另一个用于那些不能使用的服务器。

这就是所需要的,现在一切正常。 🙂


请注意 ,这只是基于我自己的观察,我可能是错的。 可能有些东西可能会丢失,并且可能有更好的方法来解决这个问题。 如果您发现这种情况,请随时发表评论或发布您的解决scheme。