运行服务时出错 [英] Getting error while running Service
问题描述
我创建了 WCF 服务,但在运行该服务时出现以下错误:错误:
<块引用>添加服务失败.服务元数据可能无法访问.制作确保您的服务正在运行并且暴露元数据.
错误详情:
警告:没有生成代码.如果你试图生成一个客户端,这可能是因为元数据文件不包含任何有效的合同或服务或服务,因为所有合同/服务被发现存在于/reference 程序集中.核实您传递了所有元数据文档到工具.警告:如果您想生成数据合约从模式确保使用/dataContractOnly 选项.
代码:
命名空间 WCFTest{[服务合约]公共类员工详细信息{【经营合同】公共列表<员工>获取详细信息(){列表<员工>emp = new List(){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"}};返回emp;}}//使用以下示例中所示的数据协定将复合类型添加到服务操作.[数据合约][KnownType(typeof(Employee))]公开课人{[数据成员]公共字符串 Fname { 获取;放;}[数据成员]公共字符串 Lname { 获取;放;}}[数据合约]公共类员工:人{[数据成员]公共 int EmpId { 获取;放;}[数据成员]公共字符串 Desg { 获取;放;}}}命名空间 WCFTest{//注意:您可以使用重构"菜单上的重命名"命令来一起更改代码、svc 和配置文件中的类名Service1".公共类 Service1{公共列表<员工>获取数据(整数值){EmployeeDetails ed = new EmployeeDetails();返回 ed.GetDetails();}}}
但是我可以看到元数据暴露在 web.config 中.
Web.config:
<预><代码><配置><system.web><编译调试="true" targetFramework="4.0"/></system.web><system.serviceModel><行为><服务行为><行为><!-- 为避免泄露元数据信息,在部署之前将下面的值设置为 false 并删除上面的元数据端点 --><serviceMetadata httpGetEnabled="true"/><!-- 要接收故障中的异常详细信息以进行调试,请将以下值设置为 true.在部署前设置为 false 以避免泄露异常信息 --><serviceDebug includeExceptionDetailInFaults="false"/></行为></serviceBehaviors></行为><serviceHostingEnvironment multipleSiteBindingsEnabled="true"/></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/></system.webServer></配置>知道我哪里出错了吗?
我认为错误背后的原因是我将类用作服务合同,现在当我将其更改为接口时,事情按预期工作,如果我指定类即服务合同.
我在您的配置中没有看到任何
节点 - 您根本没有配置服务 - 所以没有什么可以连接的.
您需要扩展您的配置以包含如下内容:
<预><代码><配置><system.serviceModel><服务><服务名称="WCFTest.EmployeeDetails"><端点名称=默认"地址=/默认"binding="basicHttpBinding" bindingConfiguration=""contract="WCFTest.EmployeeDetails"/><endpoint kind="mexEndpoint" address="/mex"/></服务></服务></system.serviceModel></配置>现在,您拥有一个带有服务和元数据端点的服务,现在您的 WCF 测试客户端应该能够找到要连接的对象....
I have created WCF service but while running the service I am getting below error: Error:
Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.
Error Details:
Warning: No code was generated.If you were trying to generate a client, this could be because the metadata documents did not contain any valid contracts or servicesor because all contracts/services were discovered to exist in /reference assemblies. Verify that you passed all the metadata documents to the tool.Warning: If you would like to generate data contracts from schemas make sure to use the /dataContractOnly option.
Code:
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();
}
}
}
However I could see metadata is exposed in 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>
Any clue where I am going wrong?
EDIT: I think the reason behind the error is I am using a class as a service contract, now when I change it to an Interface, things work as expected, not sure why I am getting the error if I am specifying the class as service contract.
I don't see any <services>
node in your config - you're not configuring a service at all - so there's nothing there to connect to.
You need to extend your config to include something like this:
<configuration>
<system.serviceModel>
<services>
<service name="WCFTest.EmployeeDetails">
<endpoint name="Default"
address="/default"
binding="basicHttpBinding" bindingConfiguration=""
contract="WCFTest.EmployeeDetails" />
<endpoint kind="mexEndpoint" address="/mex" />
</service>
</services>
</system.serviceModel>
</configuration>
Now, you have a service with a service and a metadata endpoint, and now your WCF Test Client should be able to find something to connect to....
这篇关于运行服务时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!