返回数据时WCF失败

时间:2012-08-20 18:24:12

标签: c# wcf return

我首次涉足WCF。类HardDrive去收集有关本地连接驱动器的一堆WMI信息。出于某种原因,当它试图将List<HardDrive>返回给客户端时,我得到了

  

底层连接已关闭:连接意外关闭。

哪些是完全无用的错误消息之一。

我已经完成了逐步调试,因此我知道它在尝试将列表返回给客户端时崩溃了。我只是不知道为什么

我最好的猜测是与返回列表的大小有关,但是......它不会那么大,我不会想到?

我已经测试了这个返回一个基本的字符串,它工作正常。

namespace FCopyDataService
{
    public class FCopyDataService : IFCopyDataService
    {
        public List<HardDrive> GetAllHardDrives()
        {
            return HardDrive.GetHardDrives(); //this returns quite happily
        }

        public List<Partition> GetAllPartitions(HardDrive currentDrive)
        {
            return HardDrive.GetPartitions(currentDrive);
        }
    }
}

我使用以下方式从客户端调用它:

private void button1_Click(object sender, EventArgs e)
{
    FCopyDataServiceClient drives = new FCopyDataServiceClient();
    drives.Open(); //checks it's open
    List<HardDrive> receivedDrives = drives.GetAllHardDrives(); //crashes here
}

我的服务配置:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="102400" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>

我的客户端配置了一些奇怪的位,因为我一直试图解决这个问题。

<system.serviceModel>
          <bindings>
               <basicHttpBinding>
                    <binding name="BasicHttpBinding_IFCopyDataService" closeTimeout="00:01:00"
                         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                         allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                         maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                         messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                         useDefaultWebProxy="true">
                         <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                         <security mode="None">
                              <transport clientCredentialType="None" proxyCredentialType="None"
                                   realm="" />
                              <message clientCredentialType="UserName" algorithmSuite="Default" />
                         </security>
                    </binding>
               </basicHttpBinding>
          </bindings>
          <client>
               <endpoint address="http://localhost:19140/FCopyDataService.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFCopyDataService"
                    contract="FCopyDataService.IFCopyDataService" name="BasicHttpBinding_IFCopyDataService" />
          </client>
       <behaviors>
         <endpointBehaviors>
           <behavior>
             <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
           </behavior>
         </endpointBehaviors>
         <serviceBehaviors>
           <behavior >
             <serviceDebug includeExceptionDetailInFaults="true" />
             <serviceMetadata httpGetEnabled="true" />
             <dataContractSerializer maxItemsInObjectGraph="2147483647" />
           </behavior>
         </serviceBehaviors>
       </behaviors>
    </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

HardDrive类必须序列化,但它还包含对Partitions的循环引用。

所以我更改了Partitions以包含[DataContract(IsReference=true)]并将HardDrives删除为[DataMember]分区,这似乎是为了我的目的而做的,但我觉得不是'最好的答案。