我已经搜索了这个问题,并且获得了太多链接,并尝试了大部分链接,但我的问题并没有解决。我在管理模式下运行我的VS. 我创建了新项目,然后将WCF服务添加到我的项目。我想通过ajax调用我的服务。
这是我在Sevice.cs和Iservice.cs中的代码
namespace WcfWithAjax
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string GetEmployeeList();
}
}
和
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);
}
}
}
然后配置我的web.config文件 如下
<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>
现在我正在推销我的项目,但它正在显示
无法添加服务。可能无法访问服务元数据。确保您的服务正在运行并公开元数据。
这是我得到的一些链接,但无法解决我的问题
failed-to-add-a-service-service-metadata-may-not-be-accessible-make-sure-your
答案 0 :(得分:1)
更改配置代码广告跟随
<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>