【问题标题】:WCF Add Service Reference - Collection type keeps trying to use System.ArrayWCF 添加服务参考 - 集合类型不断尝试使用 System.Array
【发布时间】:2014-06-02 17:58:28
【问题描述】:

向我的 WPF 应用程序添加自定义 WCF 服务引用时遇到问题。我已经在这项服务和应用程序上工作了将近 6 个月,直到今天才遇到这个问题。每当我尝试通过“添加服务引用”添加 WCF 服务时,我所有返回 List 的方法都试图返回 T[]。当我配置服务引用并将集合类型更改为“System.Collection.Generics.List”时,它在状态栏中显示它正在更新引用代码,但是 WPF 应用程序中的方法抛出错误,说明它可以't 隐式地​​从 List 转换为 Array。这似乎是在我将更新推送到服务器之后发生的,但唯一改变的是我的一个对象的初始化方式。到目前为止,我已经尝试过以下方法;

  • 清理项目
  • 清理解决方案
  • 重建解决方案
  • 手动删除了 obj/bin/Service Reference 文件夹
  • 删除并重新发布了 wcf 服务
  • 重新启动服务器

我完全不知所措,现在不知道该怎么办。非常感谢任何帮助!

更新

我创建了一个单独的控制台项目,一旦配置为返回列表,它就会正确连接到 WCF 服务。看起来是 WPF 项目的具体问题。

更新 2

当我尝试构建时,我注意到以下错误会短暂出现。它们会在一段时间后消失,但我不知道这是否与问题有关。它们都在 App.config 文件中。

  • “bindingConfiguration”属性无效 - 根据其数据类型“serviceBindingConfigurationType”,值“BasicHttpBinding_ICustomService”无效 - 枚举约束失败。

  • “合同”属性无效 - 根据其数据类型“clientContractType”,值“CustomServerSvc.ICustomServer”无效 - 枚举约束失败。

更新 3

有趣的是,这似乎只在构建后随机发生在客户端。

根据请求添加我的 web.config。

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="UnifyServer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="DebugUnifyCS"
          connectionString="connectionString"
          providerName="System.Data.SqlClient" />
    <add name="UnifyCS"
          connectionString="connectionString"
          providerName="System.Data.SqlClient" />
  </connectionStrings>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <client />
    <bindings>
      <basicHttpBinding>
        <binding name="myBasicBinding" maxBufferPoolSize="524288" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647" receiveTimeout="00:30:00" sendTimeout="00:30:00">
          <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
            maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <!--<bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IUnifyServer" >
          <security mode="None" />
        </binding>
      </basicHttpBinding>
    </bindings>-->
      <services>
        <service name="UnifyServer.UnifyServer">
          <endpoint address="UnifyServer" binding="basicHttpBinding" bindingConfiguration="myBasicBinding" contract="UnifyServer.IUnifyServer"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="http://unifysrv2012:19081/"/>
            </baseAddresses>
          </host>
        </service>
        <service name="UnifyServer.ServiceEndpoint">
          <endpoint address="ServiceEndpoint" binding="basicHttpBinding" bindingConfiguration="myBasicBinding" contract="UnifyServer.IServiceEndpoint"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="http://unifysrv2012:19081/"/>
            </baseAddresses>
          </host>
        </service>
      </services>
      <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

  <applicationSettings>
    <UnifyServer.Properties.Settings>
      <setting name="UnifyServer_some_service" serializeAs="String">
        <value>https://some.url.com/some_service.asmx</value>
      </setting>
    </UnifyServer.Properties.Settings>
  </applicationSettings>
</configuration>

【问题讨论】:

  • 那么当你添加一个新的服务引用时,你现有的代码库会被修改吗?
  • 它看起来像是在资源管理器的服务引用文件夹下添加了相关文件,并且它确实更正了整个应用程序中所有丢失的引用。无论我将其配置为什么,它都会不断尝试将集合作为数组返回。
  • 您是否使用相同的名称/命名空间?这些会导致冲突吗?
  • 命名空间不同。客户端(WPF)应用程序不包含任何自定义对象,我只使用来自服务器的定义。
  • 您说您正在开发服务和应用程序?那么,为什么不只是在客户端中重用该界面,而不是让 Visual Studio 尝试从 wsdl 中找出它呢?您的代码将更小更快,类型正确,甚至编译速度更快。

标签: c# .net wpf web-services wcf


【解决方案1】:

这种情况经常发生,因此当您添加对 WCF 服务的引用时,请检查:

  1. 在“添加服务参考”屏幕中,按下“高级...”按钮(位于底部)。

  2. 在您刚刚打开的“服务参考设置”屏幕的中间部分,您会看到 2 个组合,您可以在其中指定 集合类型,选择正确的,然后单击确定。

【讨论】:

    【解决方案2】:

    我怀疑任何服务参考都已更新,因为您在更新服务参考时遇到错误。

    尝试从这些步骤开始,每个步骤都独立于其他步骤并且可能是问题的原因:

    • 确保为您的服务接口声明[ServiceContract],因为这会产生确切的错误。

    • 如果您重构了代码或更改了任何文件/命名空间。确保它适用于任何地方。

    • web.config 中使用完全限定的服务名称和Namespace

    • 确保System.ServiceModel & System.Runtime.Serialization 引用没有丢失。

    • 关闭VS后删除.SUO(解决方案用户选项文件)文件。这将重置 VS 中 XMLEditor 组件的缓存。


    更新

    对于错误The 'bindingConfiguration' attribute is invalid - The value 'BasicHttpBinding_ICustomService' is invalid according to its datatype 'serviceBindingConfigurationType' - The Enumeration constraint failed.

    确保所有服务接口/合约都有[ServiceContract]属性,方法有[Operation Contract]

    还要确保所有 DataContract 属性都具有 [DataMember]enum 具有 [EnumMember] 属性。


    更新 2

    我遇到的一个情况是某些属性无法序列化。例如我返回了DataSet,默认情况下 wcf 无法将其序列化为二进制,这会导致错误。

    在最坏的情况下,您必须分离数据合约并重用它们,并且必须使用 SVCUtil 手动生成代理。我知道这听起来很疯狂。但有时,当所有事情都不起作用时,它似乎正在起作用。

    让我知道它是否有效。

    【讨论】:

    • 我已经完成了上面列出的所有事情。从来没有听说过删除 SUO 文件,但我还是试过了。还是没有运气。
    • 请自行添加 web.config 和服务合同,它可以帮助人们发现可能是问题根源的东西。
    • 我的开发机器目前正在进行维护。我会在它重新上线后立即上传。感谢您的帮助。
    • Web.config 已添加。
    • 感谢更新,我发现了一些没有用 DataContract 修饰的方法,但除此之外,其他一切看起来都很干净。我检查了每一个方法和属性,以确保一切看起来都很好。最令人困惑的是它似乎特定于这个项目。如果我在这个项目的对象浏览器中浏览服务,它确实显示它正在尝试返回 T[],但 references.svcmap 文件正在尝试序列化为 List.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多