我开始使用 WCF 来编写 Web 服务,而不是旧的 asp.net asmx 方式。我希望能够返回 JSON,所以我不能用旧的方式来做。如果我返回一个字符串或 int,Web 服务工作正常,但如果我尝试返回一个 DataTable,它会变得无响应并且我什么也得不到。我已经尝试调试以查看它在哪里爆炸并且它甚至不会在断点处停止。
我的类文件如下所示:
public string XMLData(string id)
{
return "You requested " + id;
}
public string JSONData(string id)
{
return "You requested " + id;
}
public DataTable TestDT()
{
DataTable Testing = new DataTable("TestDT");
return Testing;
}
我的接口文件如下所示:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "testdt/")]
DataTable TestDT();
该json
方法以及该xml
方法都可以正常工作。当我调用 TestDT 时,我得到一个显示没有连接的标准 IE 页面。我已经用数据或数据表中没有数据进行了尝试,结果相同。
这里还有一个注意事项:当我在本地运行 WCF 服务(点击 PLAY)时,我的测试应用程序现在显示服务,我看到了这个:
我怎样才能让它与数据表一起工作?
编辑#1:我实际上解决了数据未返回的问题。我最终放弃了返回数据集的尝试,而是创建了一个锯齿状数组并最终将其作为 JSON 返回。我稍后会发布它作为答案,以便人们知道我是如何做到的。我现在更感兴趣的是第 2 部分。为什么我的 WCF 测试客户端没有显示任何方法(见附图)我的预感是它与 web.config 相关,所以我在下面发布:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="DBDjuggleWS" connectionString="Data Source=localhost;Initial Catalog=PrayUpDev;Persist Security Info=True;User=DjuggleMaster;Password=DJPassword!" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name ="PrayUpService.PrayUp" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="PrayUpService.IPrayUp" behaviorConfiguration ="web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- 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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>