Silverlight 2,无法使用新服务更新服务参考

时间:2009-08-30 13:22:37

标签: wcf silverlight silverlight-2.0

在Silverlight 2中,我试图添加一个新服务,该服务将返回一个包含两个列表的对象,从WCF服务到客户端上的Silverlight应用程序。 svc和接口文件已包含两个正在使用和正在使用的协定。添加新服务后,单击Silverlight应用程序中的“更新服务引用”选项并收到错误:

There was an error downloading "http://localhost:3005/CMS.svc"  ...

Metadata contains a reference that cannot be resolved "http://localhost:3005/CMS.svc" ...The client and service bindings may be mismatched...

即使Web服务项目没有错误地重建,我认为我在Web服务项目中定义服务的方式肯定有问题,因为当我删除新服务时,剩下的两个服务都会更新OK,如果我添加一个我知道可以的新服务,服务引用将更新OK。所以我认为这不是端点问题,也不是端口号等问题。

新服务应该返回一个包含两个列表的对象。

以下是代码:

在界面文件中:

namespace CMSSilverlight.Web
{
  // NOTE: If you change the interface name "ICMS" here, you must also update the reference to "ICMS" in Web.config.
  [ServiceContract]
  public interface ICMS
  {

    [OperationContract]
    POCollection GetPOCollection(String s);

  }


  [DataContract]
  public class POCollection
  {
    [DataMember]
    public IList<Employee> em;
    [DataMember]
    public IList<School> sc;
  }

  public class Employee
  {
    public string EmpID { get; set; }
    public string EmpName { get; set; }
    public Employee(string empID, string empName)
    {
      this.EmpID = empID;
      this.EmpName = empName;
    }
  }

  public class School
  {
    public string SchID { get; set; }
    public string SchName { get; set; }
    public School(string schID, string schName)
    {
      this.SchID = schID;
      this.SchName = schName;
    }
  }
}

在服务文件中:

namespace CMSSilverlight.Web
{
  {

    public POCollection GetPOCollection(String sParam)
    {
      IList<Employee> empList = new List<Employee>();
      for (int i = 0; i < 5; i++)
      {
        empList.Add(new Employee(i.ToString(), i.ToString() + " Emp Name"));
      }

      IList<School> schList = new List<School>();
      for (int i = 0; i < 5; i++)
      {
        schList.Add(new School(i.ToString(), i.ToString() + " Sch Name"));
      }

      POCollection po = new POCollection()
      {
        em = empList,
        sc = schList
      };

      return po;

    }
  }
}

1 个答案:

答案 0 :(得分:0)

詹姆斯

非常感谢 - 我应该这样做。无论如何,这是错误。只需将[DataContractAttribute]属性添加到EmployeeSchool类,一切正常。这是一个令人沮丧的学习过程,但是当解决方案被揭示时它很好。

An ExceptionDetail, likely created by `IncludeExceptionDetailInFaults=true`, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: http://tempuri.org/:ICMS ----> System.Runtime.Serialization.InvalidDataContractException: Type 'CMSSilverlight.Web.Employee' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  See the Microsoft .NET Framework documentation for other supported types.