使用Excel在WCF Mex Moniker上调用方法时出现自动化错误 [英] Automation Error when invoking method on WCF mex Moniker with Excel

查看:118
本文介绍了使用Excel在WCF Mex Moniker上调用方法时出现自动化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功创建了一个服务绰号作为WCF服务的客户端.但是我无法在绰号上调用任何方法.

I successfully created a service moniker as client for my WCF service. But I'm unable to call any method on the moniker.

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

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

以下代码在Excel中创建Moniker.

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

上面的代码最多可以执行GetObject调用,这意味着已成功创建名字对象.但是,一旦尝试在其上调用任何方法,我都会收到错误消息.

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.

WCF方法可以很好地与Microsoft WCF测试客户端和其他WCF客户端一起使用.因此,我知道服务本身没有问题.

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.

推荐答案

对于面临相同问题的任何人,这里都是解决方案.通过一些研究,我自己找出了问题的原因,下面是我为解决该问题所做的工作.

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.

问题原因

(似乎)使用COM接口(使用别名字符串)连接到WCF服务的程序会遇到复杂类型(如类,结构,数组等)的问题.它们只能与简单类型一起使用(例如字符串,整数,十进制,布尔值等). 方法参数或返回类型中的复杂类型不起作用.

(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.

我的解决方案

一旦我找出导致问题的原因,就很容易找到解决方案.我只是为我感兴趣的方法创建了一个单独的WCF服务(接口),并确保没有公开公开的复杂类型,即方法定义中没有复杂类型-方法参数和返回类型.

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.

接下来,我创建了一个实现此接口的类,就像其他任何WCF服务一样.我从最初的一个类派生了该类,因此无需重复在此实现的所有业务逻辑.本课程解决的唯一目的是超越我所面临的局限性.它只是原始类的包装,其中包含有限数量的方法.这些方法简称为基类等效方法,并将结果直接返回给客户端.

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.

然后,我修改了app.config文件以承载此新服务,然后用该新服务地址替换了别名字符串中的服务地址.因此,基本上,我最终托管了两个WCF服务-一个用于可以使用复杂类型的客户端,另一个用于那些不能使用复杂类型的客户端.

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.

这篇关于使用Excel在WCF Mex Moniker上调用方法时出现自动化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆