Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - WCF method returning JSON / XML objects not working

I was new to WCF, i was trying to build a sample application using VS 2010 and code provided below

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>

and if try using the above url blank is getting displayed can some one help me ??? thanks in advance ..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do some change in interface

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

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

}

add some code on class like below

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

your web.config file like this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...