【发布时间】:2012-03-22 05:36:55
【问题描述】:
我是 WCF 新手,我正在尝试使用 VS 2010 和下面提供的代码构建示例应用程序
IProductService.cs
[ServiceContract]
public interface IProductService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
Products SelectAllProducts();
}
[DataContract]
public class Product
{
[DataMember]
public int ProductId { get; set; }
[DataMember]
public string Name { get; set; }
}
[CollectionDataContract]
public class Products : System.Collections.ObjectModel.Collection<Product>
{
}
ProductService.cs
public class ProductService : IProductService
{
public Products SelectAllProducts()
{
var products = new Products();
var prod = new Product();
prod.ProductId = 1;
prod.Name = "SAMSUNG";
products.Add(prod);
prod = new Product();
prod.ProductId = 2;
prod.Name = "RELIANCE";
products.Add(prod);
return products;
}
}
http://localhost:1050/WCFService1/ProductService.svc/SelectAllProducts
Web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
如果尝试使用上面的 url 空白正在显示,有人可以帮助我吗??? 提前谢谢..
【问题讨论】:
-
您的 WCF 服务配置是什么?也许您需要将 Products 添加到 ServiceKnownTypes。
-
有趣的是你在这里添加了你的本地主机链接,发送你的生产网址
-
@DmitriyReznik 将更新 web.config
-
@manny 我在我的本地主机上尝试过,如果这不能使用生产 URL ...
-
尝试将 KnownType 属性设置为您的数据合同类。以下是参考网址:stackoverflow.com/questions/560218/wcf-configuring-known-types