WCF服务+ SvcUtil工具生成意外的对象结构 [英] WCF service + SvcUtil generating unexpected object structure

查看:201
本文介绍了WCF服务+ SvcUtil工具生成意外的对象结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个WCF应用程序将侦听来自供应商系统的请求。供应商为我提供了一个WSDL,所以我需要创建一个服务,并公开其端点他们。

I am trying to create a WCF application that will listen for requests from a suppliers system. The supplier has provided me with a WSDL so I need to create a service and expose its' endpoint to them.

我已经使用svcutil.exe的生成C#类,但它输出相当古怪的类型。

I have used SvcUtil.exe to generate the C# classes, but it outputs rather odd-looking types.

这是已经给我的WSDL的片段:

This is a snippet of the WSDL that has been given to me:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified">
      <s:element name="Submit">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="Incident">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="TransactionId" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="TransactionType" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="TransactionSubType" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="unbounded" form="unqualified" name="ConfigurationItem">
                    <s:complexType>
                      <s:sequence>
                        <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="AssetTag" type="s:string" />
                        <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="Name" type="s:string" />
                    ....



我运行的命令很简单

The command I run is simply

svcutil.exe file_name.wsdl

我预计这将创建一个这样的结构:

I would expect that this creates a structure like this:

class Submit { ... }
class Incident { ... }
class ConfigurationItem { ... }

因此,当它是系列化我得到的是这样的:

So that when it is serialized I get something like:

<Submit>
    <Incident>
        <TransactionId>12345</TransactionId>
        <TransactionType>12345</TransactionType>
        <TransactionSubType>12345</TransactionSubType>
        <ConfigurationItem>
            <AssetTag>xyz</AssetTag>
            <Name>name</Name>
        </ConfigurationItem>
    </Incident>
</Submit>



不过,svcutil.exe的输出给了我以下内容:

However, the output of SvcUtil.exe gives me the following:

class SubmitIncident { ... }
class SubmitIncidentConfigurationItem { ... }

这似乎是一堆了提交和事件为对象之一,也是它前添加SubmitIncident'到每个嵌套元素。所以,当序列化,我得到这样的:

It's seems to bunch up the Submit and the Incident objects into one, and also it prepends 'SubmitIncident' onto each of the nested elements. So when serializing I get this:

<SubmitIncident>
    <TransactionId>...</TransactionId>
    <TransactionType>...</TransactionType>
    <TransactionSubType>...</TransactionSubType>
    <SubmitIncidentConfigurationItem>
        <AssetTag>...</AssetTag>
        <Name>...</Name>
    </SubmitIncidentConfigurationItem>
</SubmitIncident>

这会导致这两个供应商的问题(我的服务不符合他们的WSDL所以他们不能跟我说话),与以后的处理我的申请正在做的。谁能帮助我了解为什么发生这种情况,如何阻止它,并得到SvcUtil工具输出正常。

This causes problems with both the supplier (my service doesn't match their WSDL so they can't talk to me), and with onward processing I am doing in the application. Can anyone help me understand why this is happening, how to stop it, and get SvcUtil to output properly.

推荐答案

原来,这个类的定义和定义是不是真正的问题在这里(它后来导致另一个问题,但一旦你明白这是怎么回事这是很容易分辨太)。这个问题的根源是什么我已在一次性开发阶段完成的。

It turns out that the class definitions and definitions aren't really the issue here (it does cause another problem later but that's easily resolvable too once you understand what's going on). The issue was rooted in something I had done in the throwaway-development phase.

我做的第一件事就是使用 svcutil.exe的来生成使用从供应商的WSDL的服务器类。有一次,我有足够的启动调试/测试,当然我走向WCF测试客户端,并内置在开发服务器Visual Studio的。当使用客户端,它不停地想出各种晦涩的错误,例如, HTTPS不支持,其中许多被配置不一致问题,以便固定用Google检索合同和绑定等问题,但这些错误当中是这样的:

The very first thing I did was use svcutil.exe to generate the server classes using the WSDL from the supplier. Once I had enough to start debugging/testing, naturally I headed for the WCF Test Client and Visual Studio's built in development server. When using the client, it kept coming up with all sorts of obscure errors, e.g. https not supported, problems with contracts and bindings, etc. Many of them were configuration inconsistencies so were fixed with a bit of Googling, but amongst the errors was this:

的操作是不是在WCF测试客户端支持,因为它使用类型SubmitIncident

这是怎么回事,当任何<$的C $ C> SubmitIncident 的属性是类型对象(即使嵌套在其他属性低了下去)。基本上,我只是工作在一个错误的时间,拉出未直到最后它的工作位。 主要是我添加和删除的属性和类,例如属性数据成员,SOAPACTION,命名空间,等等。

This was happening when any of SubmitIncident's properties were of type object (even if nested lower down in other properties). Basically I was just working on one error at a time, pulling out bits that didn't work until eventually it did. Mainly I was adding and removing Attributes on properties and classes, e.g. DataMember,SoapAction, Namespace, etc.

最后,我是能够成功地调用端点使用WCF测试客户端应用程序。然而,当它来到它通信的其他应用程序,这是没有按照原来的问题。 !我做得到它在WCF测试客户端工作的变化实际上是由该服务无效原始WSDL架构

Eventually I was able to successfully invoke the endpoints in the application using the WCF Test Client. However when it came to other applications communicating to it, it was failing as per the original question. The changes I made to get it working in the WCF Test Client actually made the service invalid to the original WSDL schema!

TL; DR;

不要建立一个使用WCF测试客户端来测试他们你的WCF应用程序。我不能强调这一点。它迫使你删除的东西,会导致应用程序在正常使用中破裂。甚至从早期开发阶段使用行业标准的应用程序一样了SoapUI。

Do not build your WCF applications using the WCF Test Client to test them. I can't stress this enough. It forces you to remove things that will cause your application to break in normal use. Use an industry standard application like SoapUI even from the early development phase.

相关链接

  • http://blogs.msdn.com/dotnetinterop/archive/2008/09/24/wsdl-first-development-with-wcf.aspx
  • http://wscfblue.codeplex.com/

我很乐意帮助任何问题与此有。

I am happy to help with issues anyone is having with this.

这篇关于WCF服务+ SvcUtil工具生成意外的对象结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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