无法添加服务。可能无法访问服务元数据。确保您的服务正在运行并公开元数据

时间:2012-10-02 05:35:49

标签: asp.net web-services

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

1 个答案:

答案 0 :(得分:0)

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

<endpoint address ="" binding="webHttpBinding" 
          contract="RestService.IRestServiceImpl" behaviorConfiguration="web">