【问题标题】:Combine old WCF service with new WCF service Almost There将旧 WCF 服务与新 WCF 服务结合起来
【发布时间】:2013-10-08 10:42:43
【问题描述】:

我在 IIS 上有一个 WCF 服务,一些 .net Web 应用程序正在使用该服务。我的任务是编写一个新的 WCF 服务,要求现有的 Web 应用程序可以使用新服务而无需更改它们的web.config。

所以我认为我的新服务需要 2 个接口?我已经做到了,我有三个接口 - ILocationsWCF(与旧服务中的接口同名)ILocationDW(有新方法)和

ILocationService : ILocationsWCF, ILocationDW.

然后公开课LocationService : ILocationService。我可以编写一个使用 ILocationService 的新 Web 应用程序,但我不知道如何让这个新服务有 2 个端点,一个用于旧应用程序,一个用于新应用程序(这样做是因为旧服务有点尴尬,所以我想将它们分开,然后在有机会时使用新服务重新部署旧应用程序)。大多数情况下,这种变化是由新的源数据驱动的——但我离题了。

这是我得到的错误:

绑定实例已关联到侦听 URI http://localhost:10737/LocationService.svc。如果两个端点想要共享同一个 ListenUri,它们也必须共享同一个绑定对象实例。两个冲突的端点要么在 AddServiceEndpoint() 调用中指定,要么在配置文件中指定,要么在 AddServiceEndpoint() 和 config 的组合中指定。

我对 web.config 服务模型的尝试:

  <system.serviceModel>
<services>
  <service name="PPS.Services.Location.LocationService" behaviorConfiguration="LocationServiceBehavior">
    <endpoint address="" 
              binding="basicHttpBinding" name="PPS.Services.Location.LocationService"
              bindingNamespace="PPS.Services.Location" 
              contract="PPS.Services.Location.ILocationService" 
              bindingConfiguration="BasicHttpBinding_ILocationService" 
              behaviorConfiguration="HttpBehavior">
    </endpoint>
    <endpoint address=""
              binding="basicHttpBinding" name="PPS.Services.Location.LocationsWCF"
              bindingNamespace="PPS.Services.Location"
              contract="PPS.Services.Location.ILocationsWCF"
              bindingConfiguration="BasicHttpBinding_ILocationsWCF"
              behaviorConfiguration="HttpBehavior">
    </endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="LocationServiceBehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>     
  <endpointBehaviors>
    <behavior name="HttpBehavior" />
  </endpointBehaviors>
</behaviors>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ILocationService" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"></binding>
    <binding name="BasicHttpBinding_ILocationsWCF" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"></binding>
  </basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

我的界面:

namespace PPS.Services.Location
{
[ServiceContract(Name = "LocationService")]
public interface ILocationService : ILocationsWCF, ILocationServiceDW
{...

namespace PPS.Services.Location
{
[ServiceContract(Name = "LocationsWCF")]
public interface ILocationsWCF
{...

namespace PPS.Services.Location
{
[ServiceContract(Name = "LocationServiceDW")]
public interface ILocationServiceDW
{...

对这些端点有什么帮助,还是我走错了方向?

编辑——新问题!

感谢您的帮助,marc_s 让我度过了难关。现在,我的目标是通过仅更改 web.config 中的端点来用新服务替换现有服务。我无法让它工作,我收到如下错误:

...由于 EndpointDispatcher 的 ContractFilter 不匹配,无法在接收方处理...

如果我从应用程序中删除旧服务并用新服务替换它,然后编译并运行它可以工作 - 但我不想重新部署所有旧应用程序,我宁愿替换web.config 中的端点。我什至可以这样做吗?这两种服务存在差异,主要是一个新数据库(我们的学生数据现在由一个新供应商提供——我无法控制),而且我学到了很多东西,并且能够编写更好的服务。 我可以在这里做我想做的事,还是需要运行 2 个服务,直到我可以将所有旧应用程序移动到新服务? 请注意,当我确定合同等相同时,但如果您需要查看文件,请告诉我哪些文件。 谢谢。

【问题讨论】:

    标签: wcf c#-4.0 .net-4.5 wcf-binding


    【解决方案1】:

    一个端点 = 一份合约。如果您已将两组服务方法组合成一个单一服务合同 (ILocationService),则您不能拥有两个单独的端点。

    你应该做的是有一个实现两个接口的服务实现类(LocationService):

    public class LocationService : ILocationsWCF, ILocationDW
    

    现在,您有一个服务实现,但您可以定义两个单独的端点:

    <services>
      <!-- the name= must exactly match the name of the concrete service implementation class -->
      <service name="PPS.Services.Location.LocationService"   
               behaviorConfiguration="LocationServiceBehavior">
    
         <!-- the contract= must exactly match the name of an existing service contract -->
         <endpoint name="PPS.Services.Location.LocationService"
             address="" 
             behaviorConfiguration="HttpBehavior">
             binding="basicHttpBinding" bindingNamespace="PPS.Services.Location" 
             bindingConfiguration="BasicHttpBinding_ILocationService" 
             contract="PPS.Services.Location.LocationServiceDW" />
    
         <!-- the contract= must exactly match the name of an existing service contract -->
        <endpoint name="PPS.Services.Location.LocationsWCF"
            address="someother"
            behaviorConfiguration="HttpBehavior" 
            binding="basicHttpBinding" 
            bindingConfiguration="BasicHttpBinding_ILocationsWCF"
            bindingNamespace="PPS.Services.Location"
            contract="PPS.Services.Location.ILocationsWCF" />
      </service>
    </services>
    

    现在您有两个端点 - 每个都公开一个服务合同 - 请注意:它们必须具有不同 address=..... 值!同一个地址不能有两个不同的端点

    【讨论】:

    • 这很漂亮。感谢您的解释和示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2012-01-14
    • 1970-01-01
    相关资源
    最近更新 更多