Silverlight 2,无法使用新服务更新服务引用

发布于 2024-08-03 13:04:28 字数 2152 浏览 3 评论 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服务项目中,因为当我删除新服务时,其余两个服务都会更新正常,如果我添加一个我知道正常的新服务,服务引用也会更新正常。所以我不认为这是端点或端口号等的问题。

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

代码如下:

在接口文件中:

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;

    }
  }
}

In Silverlight 2, I am attempting to add a new service which will return an object containing two lists from the WCF Service to the Silverlight app on the client. The svc and interface file already contain two contracts which work and are being used. After adding the new service, I click on the "Update Service Reference" option in the Silverlight app and receive the error:

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...

Even though the web service project rebuilds without error, I think there must be something wrong with the way I have defined the service in the web service project, because when I remove the new service, the remaining two services are updated OK, and if I add a new service which I know is OK, the service reference will update OK. So I don t think it is a problem of endpoints, or the port number, etc.

The new service is supposed to return an object which contains two lists.

Here is the code:

In the interface file:

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;
    }
  }
}

In the service file:

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;

    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2024-08-10 13:04:28

詹姆斯,

非常感谢——我应该想到这一点。无论如何,下面是错误。只需将 [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.

James,

Many thanks - I should have though of that. At any rate below was the error. It was just a matter of adding the [DataContractAttribute] attribute to the Employee and School classes, and everything worked fine. This is a frustrating learning process, but it is nice when a solution is revealed.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文