[英]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.
我在管理员模式下运行VS。 I created new project then add WCF service to my project .I want to call my service through ajax.
我创建了一个新项目,然后将WCF服务添加到我的项目中。我想通过ajax调用我的服务。
this is my code in Sevice.cs and Iservice.cs 这是我在Sevice.cs和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 然后将我的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>
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 未能对添加一种服务,服务元数据可以-不被访问的,化妆肯定,你的
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.