从 WSDL 使用 WCF 托管服务 - SVCUtil 为方法生成详细类型 [英] Hosting a service with WCF from WSDL - SVCUtil generates verbose types for methods
问题描述
我有一个来自已发布的 ASMX Web 服务的 WSDL 文件.我在追求什么正在创建一个模拟服务来模拟真实服务以进行测试.
I have a WSDL file from a published ASMX web service. What I am after is creating a mock service that mimics the real service for testing purposes.
从 WSDL,我使用 SvcUtil.exe 生成代码.显然它也会产生服务器端接口.
From the WSDL, I used SvcUtil.exe to generate code. Apparently it also generates the server side interface.
问题在于它生成了非常粗大的界面.例如,一个方法int Add(int, int)
在生成的 .cs 文件中显示为 AddResponse Add(AddRequest)
.AddRequest
和 AddResponse
有一个 AddRequestBody
和 AddRequestResponse
等等.
Well the issue is that it generates very chunky interfaces. For example, a method
int Add(int, int)
is showing up in the generated .cs file as AddResponse Add(AddRequest)
. AddRequest
and AddResponse
have a AddRequestBody
and AddRequestResponse
and so on.
问题是,为了实现,我需要为每个方法创建主体和响应实例,即使我只想返回一个简单的 int
结果.
The issue is that, to implement, I need to create the body and response instances for each method, even when I just want to return a simple int
result.
为什么不能正确生成方法签名?有没有更好的方法从 WSDL 生成 WCF 服务器端接口/合同?
Why can't it generate the method signature properly? Is there a better way of generating WCF Server side interface/contracts from WSDL?
推荐答案
你所描述的消息结构是由两件事引起的:
The message structure you are describing is caused by two things:
- 更好的跨网络服务堆栈及其编程模型的互操作性(RPC 与消息传递);
- 在现有网络服务中适应新参数的灵活性.
您不是第一个抱怨它的人,或者
You are not the first one to complain about it, or the last. It's a WSDL binding style commonly called the document/literal wrapped pattern. It produces document/literal web services and yet also supports an RPC programming style. It's very "WS interoperability friendly", so to speak...
WS-I Basic 配置文件指定 soap:body
必须只有一个子元素,在这种情况下,它是正在调用的操作名称的包装器.作为最佳实践,调用的参数仅打包到一个元素中,因为它对以后的更改更加灵活.在 WCF 的情况下,您通常会得到 一个 MessageContract,其中包含一个 MessageBodyMember 包装了所有参数.
The WS-I Basic profile specifies that the soap:body
must have only one child element and in this case it's a wrapper for the operation name that's being invoked. The parameters of the call are packed into only one element as a best practice since it's more flexible to later changes. In case of WCF you usualy end up with a MessageContract which has one MessageBodyMember which wraps all the parameters.
基本上,您看到的是很久以前的 Web 服务斗争的结果.
Basically, you are seeing the results of web service battles fought long time ago.
这里有一些关于这个主题的额外阅读(这只是冰山一角):
Here is some extra reading on the subject (and that's just the tip of the iceberg):
这篇关于从 WSDL 使用 WCF 托管服务 - SVCUtil 为方法生成详细类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!