如何在实现多个合同时拥有多个WCF服务端点?

时间:2012-08-14 17:49:06

标签: c# wcf rest service endpoint

这是我的服务类,它实现了一切:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class RESTservice : IRESTservice, IRESTservice2
{
    List<Person> persons = new List<Person>();
    int personCount = 0;

    public Person CreatePerson(Person createPerson)
    {
        createPerson.ID = (++personCount).ToString();
        persons.Add(createPerson);
        return createPerson;
    }

    public List<Person> GetAllPerson()
    {
        return persons.ToList();
    }

    public List<Person> GetAllPerson2()
    {
        return persons.ToList();
    }

    public Person GetAPerson(string id)
    {
        return persons.FirstOrDefault(e => e.ID.Equals(id));
    }

    public Person UpdatePerson(string id, Person updatePerson)
    {
        Person p = persons.FirstOrDefault(e => e.ID.Equals(id));
        p.Name = updatePerson.Name;
        p.Age = updatePerson.Age;
        return p;
    }

    public void DeletePerson(string id)
    {
        persons.RemoveAll(e => e.ID.Equals(id));
    }
}

(两份合同都工作正常)

这是我的网站。配置文件:

    <?xml version="1.0"?>
    <configuration>

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
        </serviceHostingEnvironment>
        <services>
          <service name="RESTservice">
            <endpoint address="RestService" binding="webHttpBinding" contract="test.IRESTservice" />
            <endpoint address="RestService2" binding="webHttpBinding" contract="test.IRESTservice2" />
          </service>
        </services>
        <standardEndpoints>
          <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"></standardEndpoint>
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>

    </configuration>

当然还有我的服务声明:

     RouteTable.Routes.Add(new ServiceRoute("RestService", new WebServiceHostFactory(), typeof(RESTservice)));

在localhost上执行http GET时收到以下异常:port / RestService:

服务'RESTservice'实现多个ServiceContract类型,配置文件中没有定义端点。 WebServiceHost可以设置默认端点,但前提是该服务仅实现单个ServiceContract。将服务更改为仅实现单个ServiceContract,或者在配置文件中显式定义服务的端点。

我不知道出了什么问题。任何线索?

1 个答案:

答案 0 :(得分:1)

IRESTservice和IRESTservice2声明必须将ServiceContractAttribute ConfigurationName设置为与配置文件中相同的名称:

[System.ServiceModel.ServiceContractAttribute(ConfigurationName="test.IRESTservice")
public interface IRESTService
{
}

[System.ServiceModel.ServiceContractAttribute(ConfigurationName="test.IRESTservice2")
public interface IRESTService2
{
}