为什么我的WCF服务无法返回自定义对象?

时间:2013-05-23 13:25:40

标签: c# wcf object serialization svcutil.exe

所以我的问题很像这个:WCF service: Returning custom objects,但是,尽管在这个问题上有解决方案,我看不到任何有用的东西。

昨天我提出了一个与此问题相关的问题,但如果没有找到我的问题的潜在来源,我会更接近。

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

是否存在我缺少的设置,或者我是否有错误地为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);

2 个答案:

答案 0 :(得分:0)

如果您要在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项目的配置。

答案 1 :(得分:0)

最终由于出现以下情况而导致服务器端代码抛出异常:

int.Parse("0");

这是问题的唯一原因。当我浏览跟踪日志并看到StringFormatException对我来说没有任何意义时,但是当我逐步完成代码时,显然会明白这个异常被抛出的位置/时间/原因。

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