I am making CRUD operations upon Northwind database with WCF.
First i created POST method which works when i try it with WCF test client but get method is showing this error :
Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.
I don't know if it is up to making me ViewModel that will have same properties as Employees class and then iterate over it and display results?
Here is config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" sendTimeout="00:05:00" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:55658/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
And here is get method:
public IEnumerable<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
NorthwindContext db = new NorthwindContext();
list = db.Employees.ToList();
return list;
}
this is service:
[ServiceContract]
public interface IService1
{
[OperationContract]
IEnumerable<Employee> GetEmployees();
[OperationContract]
void InsertEmployee(Employee e);
[OperationContract]
void UpdateEmployee(Employee e);
[OperationContract]
void DeleteEmployee(int id);
}
UPDATE
Ok i solved it, the problem was Employee class has foreign key and by that client can't "read" it and it shows error since he doesn't know how to read that property.
All i did was i made EmployeeView class and inserted properties that i want to show.
Get method looks like this now
public IEnumerable<EmployeeView> GetEmployees()
{
NorthwindContext db = new NorthwindContext();
IQueryable<EmployeeView> list = db.Employees.Select(e => new EmployeeView
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName
});
return list;
}
If employees have foreign key to another table will have parse error. You need to create another model dto for employee class
Model:
public int EmployeeId {get;set;}
public ICollection<Order> Orders{get;set;} // this causes to parse error. Because this object have ICollection<Employee> and this causes infinite loop
ModelDto:
public int EmployeeId {get;set;}
or you can create another dto if you want to send orders