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 服务,请检查您是否已在指定地址启用元数据发布。

4

1 回答 1

0

我要说这是因为您在 web.config 中没有端点地址。

<endpoint address ="" binding="webHttpBinding" 
          contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
于 2012-10-02T05:54:56.930 回答