这是代码片段,我在其中连接到我的 WCF 服务。
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(EMPLOYEE_LOG_IN);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if(entity.getContentLength() != 0) {
Reader employeeReader = new InputStreamReader(response.getEntity().getContent());
//create a buffer to fill if from reader
char[] buffer = new char[(int) response.getEntity().getContentLength()];
//fill the buffer by the help of reader
employeeReader.read(buffer);
//close the reader streams
employeeReader.close();
//for the employee json object
JSONObject employee = new JSONObject(new String(buffer));
Log.d("Response",employee.getString("FirstName"));
它在不同的情况下表现出不同的方式。这是我的 WCF 服务中的代码
public Controller GetEmployees(int personalNUmber)
{
return new Controller
{
EmployeeId = 1,
FirstName = "FirstName",
LastName = "LastName"
};
}
}
所以当我调用这个方法时一切正常,我得到了 json 格式,但是当我编辑我的方法并像这样连接到实体数据库时
FineReceiptsTestEntities _entity = new FineReceiptsTestEntities();
var t = _entity.Employees.FirstOrDefault(x => x.EmployeeID == personalNUmber);
return new Controller
{
EmployeeId = 1,
FirstName = t != null ? t.Name : " ",
LastName = "LastName"
};
Json 对象未返回。仅返回带有此类错误的html 内容
服务器在处理请求时遇到错误。请参阅服务帮助页面以构建对服务的有效请求。
但是浏览器中的两种方法都可以正常工作并返回相关数据。
这是我的web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="dev_FineReceiptsService.ControllersInfo">
<endpoint kind="webHttpEndpoint" contract="dev_FineReceiptsService.IControllersInfo" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
<connectionStrings>
<add name="FineReceiptsTestEntities" connectionString="metadata=res://*/FineTest.csdl|res://*/FineTest.ssdl|res://*/FineTest.msl;provider=System.Data.SqlClient;provider connection string="Data Source=msdev01;Initial Catalog=FineReceiptsTest;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
那么谁能告诉我使用实体检索数据时有什么问题?