Getting error while running Service
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 "Ser开发者_Python百科vice1" 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....
精彩评论