Reputation: 95
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;
}
Upvotes: 0
Views: 52
Reputation: 1044
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
Upvotes: 1
Reputation: 1307
WCF exposes what is called contracts via attributes, add the following attribute to your Get method to make it visible to the service
[OperationContract]
You can check https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontractattribute?view=netframework-4.7.2 to read more about it.
The gist of it is this
Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.
[OperationContract]
public IEnumerable<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
NorthwindContext db = new NorthwindContext();
list = db.Employees.ToList();
return list;
}
Then the next step would be to browse to the service and check what it shows in the browser ( a simple running of the service in VS should be enough) Or, if the service is already hosted, then you can browse to it from your browser.
A better service test would be to use WCF test client, this should be come default if you have installed visual studio.
Upvotes: 0