2012-09-29 46 views
0

由于某些要求,我必须为多个服务版本使用单个svc。我使用不同的命名空间为每个版本分离了接口合约。我只有一个类(部分)实现所有服务版本。使用单个SVC进行WCF服务版本控制

我的代码是如下:

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 
} 

中的应用/ MathServiceV1.cs文件:

public partial class MathService : V1.IMathService { } 

中的应用/ MathServiceV2.cs文件:

public partial class MathService : V2.IMathService { } 

中的应用/ 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> 

我的浓度ern是用MathService.svc/V1生成的客户端端点地址,但我想看到V1/MathService.svc

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

有什么建议吗?

+0

请解释您的意思,以便任何客户端都可以浏览服务? – Jeroen

+0

我的意思是说,如果地址格式在http:// localhost:8000/V1/MathService.svc而不是http:// localhost:8000/MathService.svc/V1中,enduser可以浏览该服务。 –

回答

1

关于您的400错误请求错误 - 您可能没有启用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重定向到上面提到的URI。 以下是关于在asp.net中进行Url重写的一些文档: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing