1

我第一次涉足 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>
4

1 回答 1

0

该类HardDrive必须被序列化,但它还包含对Partitions.

所以我更改Partitions为包含[DataContract(IsReference=true)]和删除HardDrives一个[DataMember]分区,这似乎对我的目的起到了作用,但我觉得这不是最好的答案。

于 2012-08-21T13:17:11.287 回答