返回 JSON/XML 对象的 WCF 方法不起作用 [英] WCF method returning JSON / XML objects not working
问题描述
我是 WCF 的新手,我试图使用 VS 2010 和下面提供的代码构建一个示例应用程序
I was new to WCF, i was trying to build a sample application using VS 2010 and code provided below
IProductService.cs
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
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 显示空白,有人可以帮助我吗???提前致谢..
and if try using the above url blank is getting displayed can some one help me ??? thanks in advance ..
推荐答案
我没有看到绑定 web.config 的服务.尝试添加如下行:
I don't see the service binding the web.config. Try adding line such as below:
<services>
<service name="[Your Namespace].ProductService">
<endpoint address="" binding="webHttpBinding" contract="[Your Namespace].IProductService" />
</service>
</services>
为 REST WCF 服务使用 webHttpBinding
很重要.您还需要附加 webHttpBehavior
- 这可以通过使用 WebServiceHostFactory 在您的 svc 文件中.例如,
Its important that you use webHttpBinding
for REST WCF Services. Also you need to attach webHttpBehavior
- that's possible by using WebServiceHostFactory in your svc file. For example,
<%@ServiceHost Language="C#" Service="[YourNameSpace].ProductService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
有关详细信息,请参见下文:
See below for more info:
http://saravananarumugam.wordpress.com/2011/03/04/simple-rest-implementation-with-webhttpbinding/
http://msdn.microsoft.com/en-us/magazine/dd315413.aspx
这篇关于返回 JSON/XML 对象的 WCF 方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!