【问题标题】:Why are the functions in web service not working?为什么Web服务中的功能不起作用?
【发布时间】:2017-02-20 09:22:56
【问题描述】:

您好,目前我正致力于在浏览器中以 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 测试工具对其进行测试时,功能正常。

在网站上测试

将函数添加到url后

任何建议都会有很大帮助,很抱歉这篇长文。

-更新- 在 youtube 视频中可能时,仍然无法在浏览器中运行服务。 使用 VS2013 网页版 SQL IIS Express

【问题讨论】:

  • 数据库中有数据吗?
  • @Sajeetharan 是的,我的数据库有记录,它正在测试客户端中工作。
  • @Chris 看看你的浏览器控制台有什么?
  • @Mairaj 抱歉,您又指的是什么?我怎么给你看?
  • 当您在浏览器中看到空白页面时,按 F12 并再次刷新页面,然后在控制台中看到错误。

标签: c# asp.net wcf


【解决方案1】:

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

以下链接应该会有所帮助,

Run WCF methods from a browser

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 2021-08-02
    • 2015-10-09
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多