Multiple services exposing single end point
I am new to WCF and want to know how do I have multiple services in one project and exposing single end point. I did some home work and realized that we can use interfaces to achieve this. But I am unable to proceed on this.
Can you all give you opinion.
Example:
- I have services like
E开发者_运维问答mployee Service
andCustomer Service
- From client I should access it like
IService.IEmployee.GetEmployee();
orIService.ICustomer.GetCustomer()
.
Hope I made it clear. Please guide me
Each service has always its own endpoint and each implemented service contract within the service requires its own endpoint as well.
You need facade in this case. You will have single service with single contract which will wrap the logic for both Employee.Service and Customer.Service.
The simplest implementation is like:
public interface ICustomerService { ... }
public interface IEmployeeService { ... }
[ServiceContract]
public interface IService : ICustomerService, IEmployeeService { ... }
// Your WCF service
public class Service : IService { ... }
Now Service
can either implement both ICustomerService
or IEmployeeService
interfaces or it can just internally create instances of some implementations and delegate calls like:
public class Service : IService
{
private CustomerService customerService;
public Service()
{
customerService = new CustomerService();
}
public Customer GetCustomer()
{
retunr customerService.GetCustomer();
}
}
If you have a service (implementation), you can expose any number of endpoints for that service. A service implementation class can implement multiple service contracts, e.g.
public class YourServiceImplementation : IEmployeeService, ICustomerService
{
.....
}
Each endpoint you define for this service implementation however requires one single contract to be associated with it - you cannot have a single endpoint that supports multiple contracts at the same time:
<services>
<service name="YourNamespace.YourServiceImplementation">
<endpoint name="Employee"
address="http://YourServer/Services/EmployeeServices.svc"
binding="basicHttpBinding"
contract="YourNamespace.IEmployeeService" />
<endpoint name="Customer"
address="http://YourServer/Services/CustomerServices.svc"
binding="basicHttpBinding"
contract="YourNamespace.ICustomerService" />
</service>
</services>
So if your client needs to access both the employee and the customer service, you would need to add two separate client-side proxies, one for each service contract.
精彩评论