【发布时间】: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 错误请求