整合 REST 完整 WCF 服务的冗余声明
我正在使用 .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
中配置两个端点?
示例
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 web.config 文件中执行此操作可能比通过代码更容易。您可以按照下面所示的部分配置来配置您的服务。我通常将服务实现与 WSDL 和 WSDL 分开。 REST 接口命名空间使配置更容易理解,但它是可选的。绑定和行为配置名称只是为了清楚地显示如何根据需要在各自的 serviceModel 元素中调整它们。
由于您不希望服务的 WSDL 版本受到 ASP.NET URL 路由的影响,因此我将其端点中的基地址设置为类似于 .../Wsdl/KeyService3.svc。
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.