"无法添加服务。服务元数据可能无法访问"

时间:2014-08-15 13:52:41

标签: wcf

我收到错误消息:无法添加服务。可能无法访问服务元数据。运行程序时,请确保您的服务正在运行并公开元数据。如果我从Product中删除ProductClass并注释掉GetProduct中的赋值并重新运行,我就可以运行该程序。我需要返回som

[ServiceContract(Namespace = "http://test.com")]
public interface IProducts
{
    [OperationContract]
    Product GetProduct(ProductRequest productRequest);
}
    [ServiceBehavior(Namespace = "http://test.com")]
public class ProductService : IProducts
{
    public Product GetProduct(ProductRequest productRequest)
    {
        Product product = new Product
        {
            id = 1,
            productName = "Xbox"
        };
        product.productSegment = new ProdSegment()
        {
            id=1,
            segment="Video Games"
        };
        return product;
    }
}
[DataContract(Namespace = "http://test.com")]
public class ProductRequest
{
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public ProductSegment productClass { get; set; }
}
[DataContract(Namespace = "http://test.com")]
public class ProductSegment
{
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public string segment { get; set; }
}
[DataContract]    
public class ProdSegment
{
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public string segment { get; set; }
}

[DataContract]
public class Product
{
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public string productName { get; set; }
    [DataMember]
    public ProdSegment productSegment { get; set; }
}

1 个答案:

答案 0 :(得分:0)

确保服务中使用的类适用[DataContract][DataMember]。 这将允许WCF生成服务元数据。有关详细信息,请参阅this article

更新(因为您已更新了代码示例) 您还应确保公开服务的元数据。这可以通过两种方式完成:

启用服务行为的元数据。通过添加此行您的服务行为配置:

<serviceMetadata httpGetEnabled="true" />

或公开元数据端点。在服务/服务下添加此行:

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>