1

I am developing a WCF Rest Service that will implement multiple contracts (at current 2). I deployed the solution to IIS under the default web site. On accessing the uri:

http://localhost/wcfrestsample/myservices/service1/customers:

I get following invalid operation exception:

Service 'MyService' implements multiple ServiceContract types, and no endpoints are defined in the configuration file. WebServiceHost can set up default endpoints, but only if the service implements only a single ServiceContract. Either change the service to only implement a single ServiceContract, or else define endpoints for the service explicitly in the configuration file.

Below is the code details:

namespace WcfRestServiceSample
{
    //Data Items...

    public class Customer
    {
        public String Address { get; set; }
        public String Name { get; set; }
    }

    public class Employee
    {
        public String EmployeeNumber { get; set; }
        public DateTime JoiningDate { get; set; }
        public Double Salary { get; set; }
    }

    //Service Contracts...

    [ServiceContract]
    public interface IMyService1
    {
        [WebGet(UriTemplate = "customers")]
        IEnumerable<Customer> GetCustomers();
    }

    [ServiceContract]
    public interface IMyService2
    {
        [WebGet(UriTemplate = "employees")]
        IEnumerable<Employee> GetEmployees();
    }

    //Service Implementation...

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MyService : IMyService1, IMyService2
    {
        public IEnumerable<Customer> GetCustomers()
        {
            return new List<Customer>
            {
                new Customer{ Address = "customer 1 address", Name = "Customer 1" },
                new Customer{ Address = "customer 2 address", Name = "Customer 2" }
            };
        }

        public IEnumerable<Employee> GetEmployees()
        {
            return new List<Employee>
            {
                new Employee{ EmployeeNumber = "Employee 1", JoiningDate = new DateTime(1995, 5, 5), Salary = 5555.55 },
                new Employee{ EmployeeNumber = "Employee 2", JoiningDate = new DateTime(1998, 8, 8), Salary = 8888.88 }
            };
        }
    }

    //The Global.asax

    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("MyServices", new WebServiceHostFactory(), typeof(MyService)));
        }
    }
}

The contents of the config file are as follows:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MyServiceBehavior" name="MyService">
        <endpoint address="service1" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService1" />
        <endpoint address="service2" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService2" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add 
        name="UrlRoutingModule" 
        type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

Am I missing something?

4

1 回答 1

0

在处理多个合约时,您根本不能使用基地址。即使端点的相对地址会为每个端点创建唯一的 URI。它像那样愚蠢。尝试在端点中提及整个地址。我认为它会起作用。

即使您的端点像:

<endpoint address="http://localhost/wcfrestsample/myservices/service1" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService1" />
<endpoint address="http://localhost/wcfrestsample/myservices/service2" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService2" />
于 2013-08-22T16:40:34.673 回答