由于某些特定的要求,我不得不对多个服务版本使用单个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
错误。
有什么建议吗?
关于您的400坏请求错误-您可能没有启用MEX,因此在没有有效负载的情况下发出请求对服务没有意义。
以下是关于启用MEX的问题:WCF如何启用元数据?启用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