为什么Web服务中的功能不起作用?

时间:2017-02-16 03:51:29

标签: c# asp.net wcf

问候,目前我正致力于在浏览器中以xml格式显示服务。

然而,它显示一个空页面,有解释原因吗?防火墙问题? 这是所有文件。

IProductService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;


namespace WCFRESTfulService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change     the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IProductService1
{
    [OperationContract]
    [WebGet]
    List<product> GetAllProducts();

    [OperationContract]
    [WebGet]
    product GetProducts(string id);

}

}

ProductService1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFRESTfulService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change     the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please   select Service1.svc or Service1.svc.cs at the Solution Explorer and start    debugging.
public class Service1 : IProductService1
{

    public List<product> GetAllProducts()
    {
        using(var db=new estocktakeEntities())
        {
            return db.products.ToList();
        }
    }


    public product GetProducts(string id)
    {
        Int32 _id = Convert.ToInt32(id);
        using(var db=new estocktakeEntities())
        {
            return db.products.SingleOrDefault(p => p.invtid == id);


        }


    }
}
}

的Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit     http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework"     type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,     EntityFramework, Version=6.0.0.0, Culture=neutral,     PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
    <services>
      <service name="myService.ProductService">
        <endpoint address="" behaviorConfiguration=""     binding="webHttpBinding" contract="MyService.IProductService"></endpoint>        
      </service>
    </services>
    <behaviors>
  <endpointBehaviors>
    <behavior name="restbehaviour">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add scheme="https" binding="basicHttpBinding"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"></serviceHostingEnvironment>
  </system.serviceModel>



  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="Model1" connectionString="data source=CHRISPHAN-HP\SQLEXPRESS;initial catalog=estocktake;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /><add name="estocktakeEntities" connectionString="metadata=res://*/productmodel.csdl|res://*/productmodel.ssdl|res://*/productmodel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CHRISPHAN-HP\SQLEXPRESS;initial catalog=estocktake;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>

当我使用WCF测试工具测试它时,功能正在运行。

在网站上测试过 The service page which is working

将功能添加到网址后 Get all product is not displaying

任何建议都将是一个很大的帮助,抱歉这篇长篇文章。

-UPDATE-  仍然无法在浏览器中运行服务,只有在youtube视频中才有可能。 运用 VS2013 for web SQL IIS Express

1 个答案:

答案 0 :(得分:0)

通常浏览器不显示xml结果,要从浏览器调用WCF方法,您需要在WCF中创建Restful服务

以下链接应该有所帮助,

<强> Run WCF methods from a browser