【问题标题】:WCF service versioning with single SVC使用单个 SVC 的 WCF 服务版本控制
【发布时间】:2012-09-29 06:43:24
【问题描述】:

由于某些特定要求,我必须为多个服务版本使用单个 svc。我已经使用不同的命名空间为每个版本分离了接口契约。我只有一个类(部分)实现所有服务版本。

我的代码如下:

namespace Application.V1
{
[ServiceContract(Namespace = "http://google.com/ApplicationService/v1.0", Name = "IMathService")]
public interface IMathService
}

namespace Application.V2
{
[ServiceContract(Namespace = "http://google.com/ApplicationService/v2.0", Name = "IMathService")]
public interface IMathService
}

Application/MathServiceV1.cs 文件:

public partial class MathService : V1.IMathService { }

Application/MathServiceV2.cs 文件:

public partial class MathService : V2.IMathService { }

Application/MathService.cs 文件:

public partial class MathService {}

我在服务 web.config 中添加了以下内容:

  <service behaviorConfiguration="ServiceBehavior" name="Application.MathService">
    <endpoint address="V1" binding="wsHttpBinding" contract="Application.V1.IMathService" />
    <endpoint address="V2" binding="wsHttpBinding" contract="Application.V2.IMathService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>

我有一个文件MathService.svc,内容如下:

<%@ ServiceHost Service="Application.MathService, Application"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"%>

如果我生成一个地址为http://localhost:8000/MathService.svc 的代理,则客户端端点生成如下:

    <client>
        <endpoint address="http://localhost:8000/MathService.svc/V1"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMathService"
            contract="MathService.IMathService" name="WSHttpBinding_IMathService">
        </endpoint>
        <endpoint address="http://localhost:8000/MathService.svc/V2"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMathService1"
            contract="MathService.IMathService1" name="WSHttpBinding_IMathService1">
        </endpoint>
    </client>

我担心的是客户端端点地址是用MathService.svc/V1 生成的,但我希望看到V1/MathService.svc

如果我浏览地址为http://localhost:8000/MathService.svc/V1 的服务,我会收到HTTP 400 Bad Request 错误。

有什么建议吗?

【问题讨论】:

标签: wcf dependency-injection versioning autofac


【解决方案1】:

关于您的 400 bad request 错误 - 您可能没有启用 MEX,因此在没有负载的情况下发出请求对服务没有意义。

这是一个关于启用 MEX 的问题: WCF How to enable metadata? 启用 MEX - 或使用适当的服务使用者来调用您的服务。

关于您的寻址 - 您无法单独使用 WCF 完成您想做的事情。因为您使用的是 IIS 托管的 WCF(我假设这是因为您使用的是 SVC 文件),所以您的 HTTP 请求必须被定向到您的 SVC 文件的位置,之后的任何内容(例如 /V1)都用于定位适当的端点。这就是它在 IIS 中的工作方式。将 /v1/ 放在文件名 (MathService.asmx) 之前会告诉 IIS 在尝试查找名为 MathService.asmx 的文件之前查找名为 /v1/ 的文件夹 - 显然它不会在那里找到任何东西!

但是,您可以在 Web.config 中安装 URL 重写器,以将您的首选 URI 重定向到上面提到的那个。 这是一些关于在 asp.net 中重写 Url 的文档: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 2010-09-07
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多