2

我正在开发 WCF 服务。我有一个服务操作Function getValues(Optional verbose as Boolean) as List(of String)

这有效:

' 首先,添加一个包含 iRM 接口的文件引用。
Dim ep3 As EndpointAddress
ep3 = New EndpointAddress("net.pipe://localhost/RM/RMPipe")
Dim netPipeRMClient As RMLib.iRM netPipeRMtClient = ChannelFactory(Of RMLib.iRM) _ .CreateChannel(New NetNamedPipeBinding, ep3)

dim foo as List(of String) = netPipeRMClient.getValues()

但是,这不起作用:

' 使用添加服务引用获取客户端类型... Dim ep3 As EndpointAddress
ep3 = New EndpointAddress("net.pipe://localhost/RM/RMPipe")
dim netPipeRMClient as RM.iRMClient = _
new RM.IRMClient(New NetPipeBinding, ep3)
Dim foo as List(of String) = netPipeRmClient.getValues()

在最后一行,我收到一个编译时错误,上面写着“未为参数指定参数verbose”。

verbose参数在我的方法签名中明确定义为可选,但在我的 WCF 服务合同中,当我使用通过“添加服务引用”创建的客户端时,它似乎不是可选的。

有任何想法吗?

4

2 回答 2

3

可选参数是 .NET 特定功能 - WCF 服务本质上是可互操作的,因此您不能依赖 .NET 特定功能。

您在 WCF 中交换的任何内容都基于 XML 模式和 WSDL。据我所知,WSDL 不支持可选参数。WCF 及其底层管道不知道这些东西 - 所以你不能在 WCF 服务中使用它们。

您需要找到一种在 WCF 服务调用中不使用可选参数的方法。

还有一些 WCF / SOA 做得不好的东西,在 OOP/.NET 中完全没问题——比如运算符重载、接口、泛型等——你总是必须考虑到 WCF 的设计目的是一个可互操作的 SOA 平台,例如它必须能够与其他语言和系统(如 PHP、Ruby 等)对话——其中一些不支持 .NET 的所有细节。

SOA 和 OOP 有时是不一致的——这只是生活中的事实。如果您想使用 SOA 和 WCF(我强烈支持这种方法),那么您需要愿意“以 SOA 方式来做”——即使这与您在 .NET 中可以做的事情和OOP 实践可能会建议。

于 2010-06-29T20:37:01.417 回答
0

如果您愿意使用ChannelFactory<...>而不是Add Service Reference您可以做这样的事情(重用您现有的服务合同接口)

... 合同 ...

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string Echo(string input = "Default!!!");
}

... 用法 ...

// you can still provide most of these values from the app.config if you wish
// I just used code for this example.

var binding = new BasicHttpBinding();
var factory = new ChannelFactory<IService1>(binding);
var endpoint = new EndpointAddress("http://localhost:8080/service1");
var channel = factory.CreateChannel(endpoint);
var resultDefault = channel.Echo();
var resultInput = channel.Echo("Input");
于 2010-06-29T21:15:45.647 回答