【问题标题】:WCF method returning JSON / XML objects not workingWCF 方法返回 JSON / XML 对象不起作用
【发布时间】: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

标签: c# asp.net wcf


【解决方案1】:

在界面上做一些改变

 [ServiceContract(Namespace = "JsonpAjaxService")]    
interface IService
{

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    method() 

}

在类上添加一些代码,如下所示

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService

你的 web.config 文件像这样

  <?xml version="1.0"?>
 <configuration>  
 <system.web>
  <compilation debug="true" targetFramework="4.0" />
  <authentication mode="None" />
  </system.web>
 <system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
 <system.serviceModel>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <standardEndpoints>
    <webScriptEndpoint>
       <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
    </webScriptEndpoint>
  </standardEndpoints>
 </system.serviceModel>
 </configuration>

【讨论】:

    【解决方案2】:

    我没有看到绑定 web.config 的服务。尝试添加如下行:

    <services>
         <service name="[Your Namespace].ProductService">
                    <endpoint address="" binding="webHttpBinding" contract="[Your Namespace].IProductService" />
         </service>
    </services>
    

    webHttpBinding 用于 REST WCF 服务很重要。您还需要附加webHttpBehavior - 这可以通过在您的svc 文件中使用WebServiceHostFactory 来实现。例如,

    <%@ServiceHost Language="C#" Service="[YourNameSpace].ProductService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    

    更多信息见下文:

    http://saravananarumugam.wordpress.com/2011/03/04/simple-rest-implementation-with-webhttpbinding/
    http://msdn.microsoft.com/en-us/magazine/dd315413.aspx

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 2013-02-10
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多