问题
How can I write a WCF web service that has single endpoint but multiple service contract?
Example:
[ServiceContract]
public interface IWirelessService
{
[OperationContract]
void AddWireless();
}
[ServiceContract]
public interface IWiredService
{
[OperationContract]
void AddWired();
}
[ServiceContract]
public interface IInternetService
{
[OperationContract]
void AddInternet();
}
Lets think like IInternetService is my main webs service and i want to implement IwiredService and IWirelessService in it, but i want to do implementation in their classes.Is this possible? How can i solve this problem?
回答1:
I have given the below example is it what you were looking for?
[ServiceContract]
public interface IWirelessService : IInternetService
{
[OperationContract]
Connection AddInternet();
}
[ServiceContract]
public interface IWiredService : IInternetService
{
[OperationContract]
Connection AddInternet();
}
public class WirelessService : IWirelessService
{
public Connection AddInternet()
{
//Get Internet the wireless way
}
}
public class WiredService : IWiredService
{
public Connection AddInternet()
{
//Get Internet the wired way
}
}
[ServiceContract]
public interface IInternetService
{
[OperationContract]
Connection AddInternet();
}
[ServiceContract]
public interface IEnterpriseApplicationService
{
[OperationContract]
void GetDataFromInternet(string url, IInternetService internetService);
}
public class InternetProviderService : IEnterpriseApplicationService
{
public HTMLResponse GetDataFromInternet(string url, IInternetService internetService)
{
Connection con = internetService.AddInternet();
return con.GetContentFromURL(url);
}
}
来源:https://stackoverflow.com/questions/9567810/single-endpoint-with-multiple-service-contracts