通过在代理类项目中添加服务引用对具有 4 个输入参数和 3 个输出参数的 WCF 服务进行重新排序

发布于 2024-12-20 05:02:40 字数 1566 浏览 6 评论 0原文

我看过 SO 和其他地方,看到了有关此问题的问题以及一些对我来说仍然没有意义的答案。

我正在重构我的工作 VStudio 2010 解决方案,其中包含:

  • 一个带有 ASMX Web 服务的项目
  • 另一个用于代理类的单独项目(除了由 添加 Web 引用生成的代码之外,这里没有代码)
  • 另一个用于客户端的单独项目(包含对 ProxyClass.dll

新的 VStudio 2010 解决方案具有:

  • 一个用于合约的 WCF 类型服务库项目(IFileService.cs) 一个用于实现合约的 WCF 服务库类型项目 (FileService.cs)
  • 另一个用于代理类的单独项目(除了由添加服务引用生成的代码之外,此处没有任何代码>
  • 客户端的另一个单独项目(包含对 WCFProxyClass.dll 的引用)

这是以 3 个输出参数结尾的合同(并且相同的实现顺序相同):

[ServiceContract(Name = "IFileService", Namespace =  "http://www.cbmiweb.com/TrimWCF/2011/11")]
public interface IFileService
{
    [OperationContract]
        public string DownloadFile(string trimURL
         , string TrimRecordNumber
         , string CallerPC
         , string RequestorID
         , out byte[] docContents
         , out string returnFiletype
         , out string returnFilename)
    {

这是在我的代理类中生成的 Add Service Reference项目:

public string DownloadFile(
  out byte[] docContents
, out string returnFiletype
, out string returnFilename
, string trimURL
, string TrimRecordNumber
, string CallerPC
, string RequestorID) 
{
    return base.Channel.DownloadFile(out docContents, out returnFiletype, out returnFilename, trimURL, TrimRecordNumber, CallerPC, RequestorID);
}

我读过的答案范围从“你无法在 WCF 中使用 out parms”到“您不应该使用添加服务引用,而是使用 svcutil.exe”到“参数的顺序无关紧要......它仍然有效”。

我对这里要做什么感到困惑(以及我做错了什么导致了这个重新安排的顺序以及为什么会发生这种情况)。

I've looked in SO and elsewhere and seen questions posed about this along with some answers that still make no sense to me in my case.

I'm refactoring my working VStudio 2010 solution which has:

  • one project with an ASMX webservice
  • another separate project for the proxy class (no code here except what is generated by Add Web Reference
  • another separate project for the client (contains a reference to the
    ProxyClass.dll

The new VStudio 2010 solution has:

  • one project of type WCF service library for the contract by itself (IFileService.cs)
    one project of type WCF service library for the implementation of the contract (FileService.cs)
  • another separate project for the proxy class (no code here except what is generated by Add Service Reference
  • another separate project for the client (contains a reference to the WCFProxyClass.dll)

Here is the contract which ends with 3 out parameters (and the implementation of same is the same order):

[ServiceContract(Name = "IFileService", Namespace =  "http://www.cbmiweb.com/TrimWCF/2011/11")]
public interface IFileService
{
    [OperationContract]
        public string DownloadFile(string trimURL
         , string TrimRecordNumber
         , string CallerPC
         , string RequestorID
         , out byte[] docContents
         , out string returnFiletype
         , out string returnFilename)
    {

Here is what Add Service Reference generated in my proxy class project:

public string DownloadFile(
  out byte[] docContents
, out string returnFiletype
, out string returnFilename
, string trimURL
, string TrimRecordNumber
, string CallerPC
, string RequestorID) 
{
    return base.Channel.DownloadFile(out docContents, out returnFiletype, out returnFilename, trimURL, TrimRecordNumber, CallerPC, RequestorID);
}

I have read answers ranging from "you cannot use out parms in WCF" to "you should not use Add Service Reference but instead use svcutil.exe" to "the order of the parameters do not matter...it will still work".

I am confused about what to do here (and what I've done wrong that led to this re-arranged order and WHY that happened).

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

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

发布评论

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

评论(1

谁把谁当真 2024-12-27 05:02:40

首先,你没有做错任何事:)。尽管客户端和服务器中的方法中的签名不同,但它们与它们将生成/消耗的消息是等效的。您可以毫无问题地使用该代理类,并且它应该也能正常工作。

为什么会发生这种情况是另一回事了 - 在服务描述 (WSDL) 中,每个(非单向)操作都有两条“消息”:一条带有输入参数,一条带有输出参数。这些消息分别包含操作的输入和输出,但 WSDL 中没有任何内容显示它们的顺序。因此,当添加服务引用或 svcutil 等工具生成客户端代理时,它只会“选择”一个顺序(首先输出参数),但代理将发送到服务的请求将与服务器期望的内容兼容(而且,代理将正确理解来自服务器的响应)。

如果想保持参数的顺序,可以自己创建代理类。为此,您可以使用 ChannelFactory 类,或者创建您自己的从 ChannelBase 派生的客户端类。但正如我之前提到的,您实际上并不需要这样做。

First of all, you haven't done anything wrong :). Even though the signatures in the methods in the client and the server are different, they're equivalent wrt the messages which will be produced / consumed by them. You can use that proxy class without any problems, and it should work just as well.

Why this happens is another story - in the service description (WSDL), there are two "messages" for each (non-one-way) operation: one with the input parameters, one with the output parameters. The messages contains respectively the input(s) and output(s) of the operation, but there's nothing in the WSDL which shows the order of them. So when a tool such as Add Service Reference or svcutil is generating the client proxy, it will simply "choose" one order (out parameters first), but the request which the proxy will send to the service will be compatible with what the server expects (and also, the response from the server will be correctly understood by the proxy).

If you want to maintain the order of the parameters, you can create the proxy class yourself. For this you can either use the ChannelFactory<T> class, or create your own client class derived from ChannelBase<T>. But you don't really need to do that, as I mentioned before.

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