本文介绍了使用单个SVC的WCF服务版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些特定要求,我必须为多个服务版本使用一个svc.我使用不同的名称空间分隔了每个版本的接口协定.我只有一个(部分)实现所有服务版本的类.

Due to some certain requirement, I've got to use a single svc for multiple service versions. I've separated the interface contract for each version using different namespaces. I have only one class (partial) implementing all the service versions.

我的代码如下:

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文件:

The Application/MathServiceV1.cs file:

public partial class MathService : V1.IMathService { }

Application/MathServiceV2.cs文件:

The Application/MathServiceV2.cs file:

public partial class MathService : V2.IMathService { }

Application/MathService.cs文件:

The Application/MathService.cs file:

public partial class MathService {}

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

I've added the following in the service 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,内容如下:

I have a file MathService.svc with the following:

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

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

If I generate a proxy with the address http://localhost:8000/MathService.svc the client endpoints are generated as below:

    <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.

My concern is that the client endpoint address is generated with MathService.svc/V1 but I'd like to see V1/MathService.svc.

如果我使用地址http://localhost:8000/MathService.svc/V1浏览服务,则出现HTTP 400 Bad Request错误.

If i browse the service with the address http://localhost:8000/MathService.svc/V1 i am getting HTTP 400 Bad Request error.

有什么建议吗?

推荐答案

关于您的400错误请求错误-您可能未启用MEX,因此在没有有效负载的情况下进行请求对服务没有意义.

Regarding your 400 bad request error - you probably dont have MEX enabled, so making a request without a payload makes no sense to the service.

以下是有关启用MEX的问题: WCF如何启用元数据?启用MEX-或使用适当的服务使用者调用您的服务.

Here is a question about enabling MEX:WCF How to enable metadata?Either enable MEX - or use a proper service consumer to call your service.

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

Regarding your addressing - you cannot do what you want to do with WCF alone. Because you are using IIS hosted WCF (I assume this because you are using an SVC file), your HTTP request must be directed to the location of your SVC file, and anything after that (/V1 for example) is used to locate the appropriate endpoint. This is just how it works in IIS. Putting the /v1/ BEFORE the file name (MathService.asmx) tells IIS to look for a folder called /v1/ before attempting to locate a file named MathService.asmx - obviously it wont find anything there!

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

However, you may be able to install a URL rewriter in your Web.config to redirect your preferred URI to the one mentioned above.Here is some documentation on Url rewriting in asp.net:http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing

这篇关于使用单个SVC的WCF服务版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 08:33