【问题标题】:WCF service showing blank in browserWCF 服务在浏览器中显示为空白
【发布时间】:2015-04-21 06:31:27
【问题描述】:

我是 WCF 新手,我已经向我的项目添加了一个示例 WCF 服务,但是当我尝试通过浏览器访问 webservice 方法时,它显示空白窗口。我在谷歌上搜索了解决方案,但在解决一个问题时出现了另一个问题,我想知道如何在浏览器窗口中以 json 格式获取服务返回的值。

我的代码是这样的:

[ServiceContract]

public interface IService
{   

    [OperationContract]
    [WebGet(UriTemplate = "DoWork", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        int DoWork();
}

类是这样的:

public class Service : IService
{
    private int counter=0;
    public int DoWork()
    {
        counter++;
        return counter;
    }
}

Web.config 是这样的:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <connectionStrings>
        <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <authentication mode="Forms">
            <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
        </authentication>
        <membership>
            <providers>
                <clear/>
                <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
            </providers>
        </membership>
        <profile>
            <providers>
                <clear/>
                <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
            </providers>
        </profile>
        <roleManager enabled="false">
            <providers>
                <clear/>
                <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
                <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
            </providers>
        </roleManager>
    </system.web>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
    <system.serviceModel>

    <services >
      <service name="Service">
        <endpoint address ="http://localhost:1726/WcfSessionMgt/Service.svc" contract ="IService" binding ="basicHttpBinding"  listenUri="/" ></endpoint>
        <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
      </service>

    </services>


    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
    </system.serviceModel>
</configuration>

【问题讨论】:

  • 尝试在浏览器上打开开发者工具(F12),查看请求时的网络流量,看看是否有任何结果甚至错误。
  • thx aram 但会变成这样:远程地址:127.0.0.1:1726 请求 URL:localhost:1726/WcfSessionMgt/Service.svc/dowork 请求方法:GET 状态代码:400 错误请求

标签: c# asp.net wcf


【解决方案1】:

尝试通过添加 Json 行为配置来指定您对行为使用 Json。如果您未指定您期望 Json 响应/请求,则默认为 XML,这可能是您看到的问题。

下面是一个 web.config 示例:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <webHttp defaultOutgoingResponseFormat="Json"/>
        </behavior>
      </endpointBehaviors>  
    </behaviors>
    <services>
      <service name="Service.Service">
        <endpoint address="http://localhost/Service/Service.svc"
                  listenUri="/" binding="webHttpBinding"
                  contract="Service.Service" 
                  behaviorConfiguration="JsonBehavior"/>
      </service>
    </services>
  </system.serviceModel> 

如果这不起作用,请尝试将以下内容添加到 web.config。它将输出一个详细的日志,可能允许您进一步调试问题:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "c:\log\Traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

如果您发布此转储中错误的详细日志,您可能会得到更好的响应。

编辑:删除 httpsGetEnabled="true"

【讨论】:

  • 将此复制到我的 web.config 但显示错误:ServiceMetadataBehavior 的 HttpsGetEnabled 属性设置为 true 并且 HttpsGetUrl 属性是相对地址,但没有 https 基地址。提供 https 基地址或将 HttpsGetUrl 设置为绝对地址。
  • 嗨,我不建议复制整个代码sn-p。其中大部分只是为了上下文。您感兴趣的是behaviourConfiguration。我已删除导致该错误的属性以避免进一步的问题。
  • 您是否包含了诊断信息并检查了日志?
  • 是的,亲爱的,但在 c:\ 中没有类似日志文件夹的东西
【解决方案2】:

通过将我的 web.config 更改为此解决。

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>

    <serviceHostingEnvironment>
      <baseAddressPrefixFilters>
          <add prefix="http://localhost:1726/WcfSessionMgt/"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">
         <endpoint address="http://localhost:1726/WcfSessionMgt/Service.svc" binding="webHttpBinding" contract="IService" behaviorConfiguration="JsonBehavior">
          <identity>
            <dns value="http://localhost:1726"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "c:\log\Traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2011-06-20
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多