【问题标题】:Consolidating redundant declarations for a REST-full WCF service整合 REST-full WCF 服务的冗余声明
【发布时间】:2011-08-21 17:19:58
【问题描述】:

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

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

全球.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's REST Help Endpoint

WCF's sample WSDL

【问题讨论】:

    标签: wcf rest .net-4.0 routetable wcf-rest-starter-kit


    【解决方案1】:

    在 web.config 文件中执行此操作可能比通过代码更容易。您可以按照如下所示的部分配置来配置您的服务。我通常将服务实现与 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>
    

    【讨论】:

    • 这是否支持 WCF 中内置的 /help 扩展? (上面链接)以及无扩展操作?
    • 嗯,我之前没有尝试将“
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多