2012-08-14 98 views
1

这里是我的服务类,它实现的一切:如何在实施多个合同时拥有多个WCF服务端点?

[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))); 

做时,我收到以下异常的HTTP GET在localhost:端口/ RestService:

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

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

回答

1

两个IRESTservice和IRESTservice2声明必须具有ServiceContractAttribute的ConfigurationName设置为相同的名称,在您的配置文件:

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

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

它说我不能使用“ServiceContractAttribute的”两次(即使在不同的接口),所以我装饰着这个相反:[ServiceContract(ConfigurationName =“test.IRESTservice”)]和[ServiceContract(ConfigurationName =“test.IRESTservice2”)]但我仍然得到相同的异常 – user1589780 2012-08-14 19:27:01

+0

随着你的解决方案,我得到以下编译错误:“Error \t 1 Duplicate 'System.ServiceModel.ServiceContractAttribute'” – user1589780 2012-08-15 08:19:23