简体   繁体   中英

WCF:Failed to add a service. Service metadata may not be accessible

I have searched this question , and got too many links ,and tried most of them , but my problem is not solved . I'm running my VS in admin mode. I created new project then add WCF service to my project .I want to call my service through ajax.

this is my code in Sevice.cs and Iservice.cs

namespace WcfWithAjax
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        string GetEmployeeList();
    }
}

and

namespace WcfWithAjax
{

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service : IService
    {
        [OperationContract]

        public string GetEmployeeList()
        {
            string str = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            IList<Employee> employeeList = new List<Employee>();
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Employee", con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    employeeList.Add(new Employee()
                    {
                        Id = dr.GetInt32(0),
                        Name = dr.GetString(1),
                        Position = dr.GetString(2),
                        Age = dr.GetInt32(3),
                        Salary = dr.GetInt32(4)
                    });
                }
                con.Close();
            }
            JavaScriptSerializer objJson = new JavaScriptSerializer();
            return objJson.Serialize(employeeList);
        }

    }
}

and then configured my web.config file as follow

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>

    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WcfWithAjax.Service" behaviorConfiguration="metadataBehavior">
        <endpoint address=""  binding="webHttpBinding" contract="WcfWithAjax.IService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

now I'm Debuging my project but it's showing

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata .

Here is some link i got but unable to solve my problem

failed-to-add-a-service-service-metadata-may-not-be-accessible-make-sure-your

Link 2

Change you config code ad follow

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ServiceAspNetAjaxBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WcfWithAjax.Service" behaviorConfiguration="metadataBehavior">
        <endpoint address=""  binding="webHttpBinding" contract="WcfWithAjax.IService" behaviorConfiguration="ServiceAspNetAjaxBehavior"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM