2011-05-09 32 views
0

我使用.NET 4 WCF,露出下面的REST的Web服务满合并冗余声明的REST全WCF服务

[OperationContract] 
    [WebGet] 
    void Test(); 

由于这是一个面向开发人员的程序,我要支持REST-完整的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))); 

问题

由于我不喜欢在两个位置声明服务,所以d o我可以在配置中配置两个端点,或者在Application_Start中配置两个端点?

例子

WCF's REST Help Endpoint

WCF's sample WSDL

回答

0

它可能更容易在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> 
+0

这会支持内置于WCF的/ help扩展吗? (以上链接),也可以无延伸操作? – LamonteCristo 2011-05-09 15:17:26

+0

嗯,我还没有尝试混合REST工具包配置像“ 2011-05-09 16:11:24