WCF服务 - 运行时未在界面上看到ServiceContract

Mik*_*e K 15 service wcf binding interface servicehost

我是WCF的新手并试图让我的第一个服务运行.我很接近,但坚持这个问题.

在我的界面定义文件中,我有这个:

[ServiceContract(Namespace="http://mysite.com/wcfservices/2009/02")]       
    public interface IInventoryService
    {
        [OperationContract]
        string GetInventoryName(int InventoryID);
    }
Run Code Online (Sandbox Code Playgroud)

然后我有我的类文件(用于服务)继承它:

   public class InventoryService : IInventoryService
    {
        // This method is exposed to the wcf service
        public string GetInventoryName(int InventoryID)
        {
            return "White Paper";
        }
Run Code Online (Sandbox Code Playgroud)

最后,在我的Host项目中,我有这个:

    ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
    host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
        "net.tcp://localhost:9000/GetInventory");
    host.Open();
Run Code Online (Sandbox Code Playgroud)

一切都编译得很好,当主机去添加服务端点时,它会轰炸:"合同类型Inventory.InventoryService不归属于ServiceContractAttribute.为了定义有效合同,指定类型(合同接口或服务) class)必须归因于ServiceContractAttribute."

我知道我在这里缺少一些简单的东西.我将界面明确标记为服务合同,并在Host项目中引用该项目.

REA*_*REW 25

ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.InventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();
Run Code Online (Sandbox Code Playgroud)

如果您的ServiceContract属性在接口上而不是具体类,请尝试以下操作:

ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.IInventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();
Run Code Online (Sandbox Code Playgroud)