2012-03-22 33 views
-1

我是新來的WCF,我試圖建立使用VS 2010和下面的代碼WCF方法返回不能正常工作

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提供了一個示例應用程序JSON/XML對象

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> 

如果嘗試使用上面的網址空白顯示可以幫助我嗎? 在此先感謝..

+0

什麼是你的WCF服務配置? 也許您需要將產品添加到ServiceKnownTypes。 – 2012-03-22 05:44:08

+0

有趣的部分是你已經在這裏添加您的本地主機鏈接,發送您的製作網址 – manny 2012-03-22 05:45:06

+0

@DmitriyReznik虐待更新web.config – Madhu 2012-03-22 06:11:51

回答

0

我沒有看到服務綁定web.config。嘗試添加如下所示的行:

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

對於REST WCF服務,使用webHttpBinding是很重要的。您還需要附上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

0

不要在界面的一些變化

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