0

所以我的问题很像这个:WCF service: Returning custom objects,但是,尽管该问题中有解决方案,但我看不到任何工作。

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

我不认为我正在使用的数据大小有问题。我可以将此数据发送到服务器,但我在检索它时遇到了问题。我正在返回一个名为ProjectDetailsover的对象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);
4

2 回答 2

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 项目的配置是不够的,也没有必要。

于 2013-05-23T16:20:25.580 回答
0

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

int.Parse("0");

这是问题的唯一原因。当我查看跟踪日志并发现StringFormatException它对我来说毫无意义时,但是当我单步执行代码时,它变得非常明显,即在哪里/何时/为什么抛出了这个异常。

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

于 2013-06-23T13:06:22.070 回答