Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Service - runtime not seeing the ServiceContract on Interface

I'm new to WCF and trying to get my first service running. I'm close but stuck on this problem.

In my interface definition file, I have this:

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

Then I have my class file (for the service) that inherits it:

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

Finally, in my Host project I have this:

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

Everything compiles fine, and when the host goes to add the service endpoint, it bombs with this: "The contract type Inventory.InventoryService is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute."

I know I'm missing something simple here. I have the interface clearly marked as a service contract and there's a reference to that project in the Host project.

like image 623
Mike K Avatar asked Feb 26 '09 21:02

Mike K


People also ask

What does the ServiceContract attribute do in this interface class?

Remarks. Use the ServiceContractAttribute attribute on an interface (or class) to define a service contract. Then use the OperationContractAttribute attribute on one or more of the class (or interface) methods to define the contract's service operations.

What is ServiceContractAttribute?

A Service Contract defines the operations that a service will perform when executed. It is a collective mechanism by which we can specify the consumer's requirements. It tells the outside world about its messages, its location and protocols which it can use for communication.

Which type of contract is applicable to interface in WCF?

There are the following two types. The Service Contract declares an interface in the WCF service for the client to get access to the interface. The Operation Contract declares a function inside the interface, the client will call this function.


1 Answers

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

If your ServiceContract attribute is on the Interface not the concrete class, try the following:

ServiceHost host = new ServiceHost(typeof(Inventory.InventoryService));
host.AddServiceEndpoint(typeof(Inventory.IInventoryService), new NetTcpBinding(),
    "net.tcp://localhost:9000/GetInventory");
host.Open();
like image 167
REA_ANDREW Avatar answered Oct 06 '22 23:10

REA_ANDREW