当我使用“添加服务引用”时,我看不到可选方法参数

发布于 2024-09-07 12:37:56 字数 1054 浏览 4 评论 0原文

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

这有效:

' 首先,添加包含 iRM 接口的文件引用。
将 ep3 调暗为端点地址
ep3 = New EndpointAddress("net.pipe://localhost/RM/RMPipe")
将 netPipeRMClient 调暗为 RMLib.iRM netPipeRMtClient = ChannelFactory(RMLib.iRM) _ .CreateChannel(New NetNamedPipeBinding, ep3)

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

但是,这不起作用:

' 使用添加服务引用来获取客户端类型... 将 ep3 调暗为端点地址
ep3 = New EndpointAddress("net.pipe://localhost/RM/RMPipe")
将 netPipeRMClient 调暗为 RM.iRMClient = _
新 RM.IRMClient(新 NetPipeBinding,ep3)
Dim foo as List(of String) = netPipeRmClient.getValues()

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

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

有什么想法吗?

I'm working on a WCF Service. I have one service operation Function getValues(Optional verbose as Boolean) as List(of String).

This works:

' First, add a file reference that contains the iRM interface.
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()

However, this does not work:

' Use Add Service Reference to get the client type...
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()

On the last line, I get a compile-time error that says "Argument not specified for parameter verbose".

The verbose parameter was clearly defined to be optional in my method signature, but in my WCF service contract, it doesn't seem to be optional when I use the client created with "Add Service Reference".

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在看孤独的风景 2024-09-14 12:37:56

可选参数是 .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 实践可能会建议。

Optional parameters are a .NET specific feature - WCF services are by nature interoperable, so you cannot rely on .NET specifics.

Anything you exchange in WCF is based on XML schema and WSDL. As far as I know, WSDL doesn't have any support for optional parameters. WCF and its underlying plumbing don't know about those things - so you cannot use them in WCF services.

You'll need to find a way to live without optional parameters in your WCF service calls.

There are a few additional things that WCF / SOA doesn't do well, that are totally ok in OOP/.NET - things like operator overloading, interfaces, generics etc. - you always have to take into account that WCF is designed to be an interoperable SOA platform, e.g. it must be able to talk to other languages and systems, like PHP, Ruby etc. - and some of those don't support all the niceties of .NET.

SOA and OOP are at odds sometimes - it's just a fact of life. If you want to use SOA and WCF (and I would strongly argue for that approach), then you'll need to be willing to "do it the SOA way" - even if that goes against what you could do in .NET and what OOP practices might suggest.

泅渡 2024-09-14 12:37:56

如果您愿意使用 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");

If you are willing to use the ChannelFactory<...> instead of the Add Service Reference you could do something like this (reusing your existing service contract interface)

... Contract ...

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

... Usage ...

// 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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文