我开始使用WCF编写Web服务,而不是使用旧的asp.net asmx方式。我想要能够返回JSON,所以我不能以旧的方式去做。如果我返回一个字符串或int Web服务工作正常,但如果我尝试返回一个DataTable它变得没有响应,我什么都没有。我尝试过调试以查看它爆炸的位置,甚至不会停在断点上。WCF WebService不返回数据表
我的类文件看起来像这样:
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页面,显示没有连接。我已经用数据或datable中的数据尝试过,结果相同。
这里的一个其他注意事项:当我在本地运行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>
考虑返回使用数据合同。如果只有一列有多行,则可以考虑返回List(字符串)。 – Baskar
[在WCF/.NET中返回DataTables]的可能重复(http://stackoverflow.com/questions/12702/returning-datatables-in-wcf-net) – tomcater