【发布时间】:2017-02-26 21:08:43
【问题描述】:
我创建了 WCF 服务,但在运行该服务时出现以下错误: 错误:
添加服务失败。服务 元数据可能无法访问。制作 确保您的服务正在运行并且 暴露元数据。
错误详情:
警告:未生成任何代码。如果您 试图生成一个客户端,这个 可能是因为元数据 文件不包含任何有效的 合同或服务或因为所有 合同/服务被发现 存在于 /reference 程序集中。核实 你传递了所有的元数据 文件到工具。警告:如果你 想生成数据合约 从模式确保使用 /dataContractOnly 选项。
代码:
namespace WCFTest
{
[ServiceContract]
public class EmployeeDetails
{
[OperationContract]
public List<Employee> GetDetails()
{
List<Employee> emp = new List<Employee>()
{
new Employee(){Fname="AA",Lname="BB",EmpId=1,Desg="A"},
new Employee(){Fname="CC",Lname="DD",EmpId=1,Desg="B"},
new Employee(){Fname="EE",Lname="FF",EmpId=1,Desg="C"},
new Employee(){Fname="GG",Lname="HH",EmpId=1,Desg="D"},
new Employee(){Fname="II",Lname="JJ",EmpId=1,Desg="A"},
new Employee(){Fname="KK",Lname="LL",EmpId=1,Desg="B"}
};
return emp;
}
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
[KnownType(typeof(Employee))]
public class Person
{
[DataMember]
public string Fname { get; set; }
[DataMember]
public string Lname { get; set; }
}
[DataContract]
public class Employee : Person
{
[DataMember]
public int EmpId { get; set; }
[DataMember]
public string Desg { get; set; }
}
}
namespace WCFTest
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1
{
public List<Employee> GetData(int value)
{
EmployeeDetails ed = new EmployeeDetails();
return ed.GetDetails();
}
}
}
但是我可以看到元数据暴露在 web.config 中。
Web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
有什么线索我哪里出错了吗?
编辑:我认为错误背后的原因是我将类用作服务合同,现在当我将其更改为接口时,事情按预期工作,不知道为什么我在指定时收到错误类作为服务合同。
【问题讨论】:
-
我在您的 web.config 中没有看到任何
节点.....另外:您如何尝试从服务创建代理:在 Visual Studio 中添加服务引用或运行svcutil 在命令行 ?? -
不,我没有尝试创建任何代理,我所做的是-创建新的->项目>WCF 应用程序,然后添加此代码,并尝试运行 WCF sln。
-
默认的 WCF 应用程序是这样工作的(我的意思是在 C# 或 web.config 中没有更改 anu 代码的默认应用程序)
-
我不是托管。我只是创建一个 wcf 应用程序然后运行 sln。现在当你运行 sln 时,你会得到对话框“WCF TEST Client”,现在这样做我得到了错误
标签: wcf