1

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

    [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:port/RestService 上执行 http GET 时出现以下异常:

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

我不知道出了什么问题。有什么线索吗?

4

1 回答 1

1

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

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

[System.ServiceModel.ServiceContractAttribute(ConfigurationName="test.IRESTservice2")
public interface IRESTService2
{
}
于 2012-08-14T18:01:31.417 回答