WCF service - ne pas voir le temps d'exécution sur ServiceContract Interface
-
09-09-2019 - |
Question
Je suis nouveau à WCF et essayer d'obtenir mon premier fonctionement. Je suis proche, mais coincé sur ce problème.
Dans mon fichier de définition d'interface, j'ai ceci:
[ServiceContract(Namespace="http://mysite.com/wcfservices/2009/02")]
public interface IInventoryService
{
[OperationContract]
string GetInventoryName(int InventoryID);
}
Alors j'ai mon fichier de classe (pour le service) qui hérite:
public class InventoryService : IInventoryService
{
// This method is exposed to the wcf service
public string GetInventoryName(int InventoryID)
{
return "White Paper";
}
Enfin, dans mon projet hôte j'ai ceci:
ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
"net.tcp://localhost:9000/GetInventory");
host.Open();
Tout compile bien, et quand l'hôte va ajouter le point de terminaison de service, des bombes avec ceci: « Le type de contrat Inventory.InventoryService est pas attribué à ServiceContractAttribute Afin de définir un contrat valide, le type spécifié (soit contrat. interface ou classe de service) doivent être attribués avec ServiceContractAttribute. "
Je sais que je manque quelque chose de simple ici. Je l'interface clairement marquée comme un contrat de service et il y a une référence à ce projet dans le projet hôte.
La solution
ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
"net.tcp://localhost:9000/GetInventory");
host.Open();
Si votre attribut ServiceContract est sur l'interface et non la classe concrète, procédez comme suit:
ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.IInventoryService), new NetTcpBinding(),
"net.tcp://localhost:9000/GetInventory");
host.Open();