【问题标题】:Why does my WCF Service have trouble returning custom object?为什么我的 WCF 服务无法返回自定义对象?
【发布时间】:2013-05-18 21:15:19
【问题描述】:

所以我的问题很像这个问题:WCF service: Returning custom objects,然而,尽管该问题有解决方案,但我认为没有任何效果。

昨天我提出了一个与此相关的问题,但如果没有确定我的问题的潜在来源,我已经接近了。

我不认为我正在处理的数据大小有问题。我可以将此数据发送到服务器,但我在检索它时遇到了问题。我在WCF 上返回一个名为ProjectDetails 的对象。但是,我遇到了从服务接收到客户端的问题。

是否有我遗漏的设置或者我为WCF 错误地配置了我的课程?

这是我的ProjectDetails 代码:

[DataContract]
public class ProjectDetails 
{
    [DataMember]
    public int projectId { get; set; }
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public int calYearId { get; set; }
    [DataMember]
    public string projectState { get; set; }
    [DataMember]
    public string reportTitle { get; set; }
    [DataMember]
    public int plantId { get; set; }
    [DataMember]
    public string holdNumber { get; set; }
    [DataMember]
    public string holdDescription { get; set; }
    [DataMember]
    public string remarks { get; set; }
    [DataMember]
    public string adminComments {get; set; }
    [DataMember]
    public int companyId { get; set; }
    [DataMember]
    public int customerId { get; set; }
    [DataMember]
    public string folderNumber { get;  set; }
    [DataMember]
    public int invoicedInFull { get; set; }
    [DataMember]
    public int approved { get; set; }
}

这是我的服务合同中定义的方式:

[ServiceContract]
public interface IProjectService
{
    [OperationContract]
    ProjectDetails GetProjectDetails(int selected_id);
}

我已经用svcutil.exe 重新生成了我的代理并更新了我的服务参考。但是,每次我调用 GetProjectDetails 方法时,我的代码都会失败。我明白了:

底层连接已关闭:接收时发生意外错误。

如果有人可以提供帮助,我将不胜感激。

更新

客户端配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IProjectService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://192.168.0.99:9000/ProjectService/ProjectService"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IProjectService"
                contract="IProjectService" name="BasicHttpBinding_IProjectService" />
        </client>
     </system.serviceModel>
</configuration>

客户端配置:

<?xml version="1.0"?>
<configuration> 
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ProjectBaseWCFServiceLib.Service1Behavior" name="ProjectBaseWCFServiceLib.ProjectService">
        <endpoint address="" binding="basicHttpBinding" contract="ProjectBaseWCFServiceLib.IProjectService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/ProjectBaseWCFServiceLib/Service1/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ProjectBaseWCFServiceLib.Service1Behavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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 -->
          <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

客户端代码:

        Console.WriteLine("Testing................");
        ProjectBaseWCFServiceLib.ProjectDetails details = client.GetProjectDetails(1);

        Console.WriteLine(details.adminComments);
        Console.WriteLine(details.approved);
        Console.WriteLine(details.calYearId);
        Console.WriteLine(details.companyId);
        Console.WriteLine(details.folderNumber);
        Console.WriteLine(details.holdDescription);
        Console.WriteLine(details.holdNumber);
        Console.WriteLine(details.invoicedInFull);
        Console.WriteLine(details.name);
        Console.WriteLine(details.plantId);
        Console.WriteLine(details.projectId);
        Console.WriteLine(details.projectState);
        Console.WriteLine(details.remarks);
        Console.WriteLine(details.reportTitle);

【问题讨论】:

  • 任何内部异常?
  • 是的,但它们都非常通用。我也收到Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host。
  • 您在代码中使用的是 linq 还是事务范围?
  • 请在您的 WCF 服务上启用跟踪 (msdn.microsoft.com/en-us/library/ms733025.aspx) 并检查跟踪日志以了解有关引发错误的原因的更多详细信息。
  • 获取完整的堆栈跟踪也会有所帮助

标签: c# wcf object serialization svcutil.exe


【解决方案1】:

如果您要在 IIS 中托管您的服务,此服务配置将适用于您

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ProjectBaseWCFServiceLib.Service1Behavior" name="ProjectBaseWCFServiceLib.ProjectService">
        <endpoint address="" binding="basicHttpBinding" contract="ProjectBaseWCFServiceLib.IProjectService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ProjectBaseWCFServiceLib.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

如果您打算让它自托管,请添加正确的基地址。但无论如何,您应该确保 ServiceConfig 位于托管服务的应用程序的 web/app.config 文件中。更新 Lib 项目的配置是不够的,也没有必要。

【讨论】:

    【解决方案2】:

    最终是服务器端代码由于发生以下情况而引发异常:

    int.Parse("0");
    

    这是问题的唯一原因。当我查看跟踪日志并看到 StringFormatException 时,这对我来说毫无意义,但当我单步执行代码时,它变得非常明显,即抛出此异常的位置/时间/原因。

    这里学到了一个很好的教训 - 调试客户端-服务器程序与经典的独立程序有很大不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多