【问题标题】:How to have multiple WCF service endpoints when implementing more than one contract?实现多个合同时如何拥有多个 WCF 服务端点?
【发布时间】:2023-03-11 18:32:01
【问题描述】:

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

    [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 时出现以下异常:

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

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

【问题讨论】:

    标签: c# wcf rest service endpoint


    【解决方案1】:

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

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

    【讨论】:

    • 它说我不能使用“ServiceContractAttribute”两次(即使在不同的界面中)所以我用这个来装饰:[ServiceContract(ConfigurationName = "test.IRESTservice")] 和 [ServiceContract(ConfigurationName = " test.IRESTservice2")] 但我仍然得到同样的异常
    • 使用您的解决方案,我得到以下编译错误:“错误 1 ​​Duplicate 'System.ServiceModel.ServiceContractAttribute'”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多