0
我正在編寫WCF服務。我的代碼是無法添加服務。服務元數據可能無法訪問。確保你的服務正在運行並且暴露元數據
namespace RestService
{
public class user
{
public user(string i, string b, string l)
{
id = i;
badgeNumber = b;
login = l;
}
public string id { get; set; }
public string badgeNumber { get; set; }
public string login { get; set; }
}
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public user[] JSONData(string id)
{
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\confiz\Desktop\att2000.mdb");
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT count(*) FROM USERINFO", connection);
int users_count = (int)command.ExecuteScalar();
command = new OleDbCommand("SELECT * FROM USERINFO", connection);
OleDbDataReader reader = command.ExecuteReader();
// string jsonString = "";
user[] users = new user[users_count];
int i=0;
while (reader.Read())
{
users[i].id = reader["USERID"].ToString();
users[i].badgeNumber = reader["Badgenumber"].ToString();
users[i].login = reader["SSN"].ToString();
/* user = new user(reader["USERID"].ToString(), reader["Badgenumber"].ToString(), reader["SSN"].ToString());
System.Web.Script.Serialization.JavaScriptSerializer jsSerializer;
jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
System.Text.StringBuilder sbUser = new System.Text.StringBuilder();
jsSerializer.Serialize(user, sbUser);
string json = sbUser.ToString();
*/
// jsonString = jsonString + json;
i++;
}
connection.Close();
return users;
}
#endregion
}
}
我的web.config文件是這樣的
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
但是當我運行這段代碼我得到無法添加服務錯誤 。服務元數據可能無法訪問。確保您的服務正在運行並展示元數據。
錯誤詳細信息:錯誤:無法從RestServiceImpl.svc獲取元數據如果這是您有權訪問的Windows(R)Communication Foundation服務,請檢查是否已啓用元數據發佈到指定地址。
我指定它像 <端點地址= 「HTTP://本地主機:35798/RestServiceImpl.svc」 結合= 「的WebHttpBinding」 合同= 「RestService.IRestServiceImpl」 behaviorConfiguration = 「網絡」> – user1673005