整合 REST 完整 WCF 服务的冗余声明

发布于 2024-11-05 18:53:57 字数 1363 浏览 6 评论 0原文

我正在使用 .NET 4 WCF 公开以下 REST-full Web 服务

  [OperationContract]
  [WebGet]
  void Test();

由于这是一个面向开发人员的程序,我希望支持 REST-full HTTP 开发人员以及喜欢使用 WSDL 的开发人员。我的方法是声明服务两次以公开传统的 WSDL 和 REST 端点:

Web.config

<serviceHostingEnvironment  aspNetCompatibilityEnabled="True" multipleSiteBindingsEnabled="true" >
  <serviceActivations >
    <add relativeAddress ="~/KeyService.svc" service="SecretDistroMVC3.Services.KeyService3"/>
  </serviceActivations>
</serviceHostingEnvironment>

Global.asax

   void Application_Start(object sender, EventArgs e)
        { 
            //  The following enables the WCF REST endpoint
            //ASP.NET routing integration feature requires ASP.NET compatibility. Please see 
            // 'http://msdn.microsoft.com/en-us/library/ms731336.aspx
            RouteTable.Routes.Add(new ServiceRoute("KeyService3", new WebServiceHostFactory(), typeof(KeyService3)));

问题

因为我不喜欢在两个位置声明服务,如何在 config 中配置两个端点,或在 Application_Start 中配置两个端点?

示例

WCF 的 REST 帮助端点

WCF 的示例 WSDL

I'm using .NET 4 WCF to expose the following REST-full webservice

  [OperationContract]
  [WebGet]
  void Test();

Since this is a developer oriented program, I want to support REST-full HTTP developers and also developers that like to use a WSDL. My approach is to declare the service twice to expose both a traditional WSDL and also a REST endpoint:

Web.config

<serviceHostingEnvironment  aspNetCompatibilityEnabled="True" multipleSiteBindingsEnabled="true" >
  <serviceActivations >
    <add relativeAddress ="~/KeyService.svc" service="SecretDistroMVC3.Services.KeyService3"/>
  </serviceActivations>
</serviceHostingEnvironment>

Global.asax

   void Application_Start(object sender, EventArgs e)
        { 
            //  The following enables the WCF REST endpoint
            //ASP.NET routing integration feature requires ASP.NET compatibility. Please see 
            // 'http://msdn.microsoft.com/en-us/library/ms731336.aspx
            RouteTable.Routes.Add(new ServiceRoute("KeyService3", new WebServiceHostFactory(), typeof(KeyService3)));

Question

Since I don't like to have the service declared in two locations, how do I either configure both endpoints in config, or both endpoints in Application_Start?

Examples

WCF's REST Help Endpoint

WCF's sample WSDL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-11-12 18:53:57

在 web.config 文件中执行此操作可能比通过代码更容易。您可以按照下面所示的部分配置来配置您的服务。我通常将服务实现与 WSDL 和 WSDL 分开。 REST 接口命名空间使配置更容易理解,但它是可选的。绑定和行为配置名称只是为了清楚地显示如何根据需要在各自的 serviceModel 元素中调整它们。

由于您不希望服务的 WSDL 版本受到 ASP.NET URL 路由的影响,因此我将其端点中的基地址设置为类似于 .../Wsdl/KeyService3.svc。

   <service name="YourNamespace.Impl.KeyService3" behaviorConfiguration="yourServiceBehaviorSettings">

        <!-- Service Endpoints -->
        <endpoint address="Wsdl"
                  binding="wsHttpBinding"
                  bindingConfiguration="Http"
                  contract="YourNamespace.Wsdl.IKeyService3" />

        <endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="IMetadataExchange"/>

        <endpoint address="Services"
              behaviorConfiguration="webBehavior"
              binding="webHttpBinding"
              name="webHttp"
              contract="YourNamespace.IKeyService3"
              listenUriMode="Explicit" />
    </service>

It's probably easier doing this in the web.config file than by code. You could configure your service along the lines of the partial config shown below. I usually separate the service implementation and the WSDL & REST interface namespaces to make the config easier to understand but it is optional. The binding and behavior configuration names are there just for clarity to show how you could tweak these as needed in their respective serviceModel elements.

Since you don't want the WSDL version of the service to be affected by the ASP.NET URL routing, I set it's base address to be something like .../Wsdl/KeyService3.svc in its endpoint.

   <service name="YourNamespace.Impl.KeyService3" behaviorConfiguration="yourServiceBehaviorSettings">

        <!-- Service Endpoints -->
        <endpoint address="Wsdl"
                  binding="wsHttpBinding"
                  bindingConfiguration="Http"
                  contract="YourNamespace.Wsdl.IKeyService3" />

        <endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="IMetadataExchange"/>

        <endpoint address="Services"
              behaviorConfiguration="webBehavior"
              binding="webHttpBinding"
              name="webHttp"
              contract="YourNamespace.IKeyService3"
              listenUriMode="Explicit" />
    </service>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文