我仍然是 wcf 的新手,总体上对 .net 不太了解。我有一个 WCF 4 Web 服务,它使用 global.asax 路由方法,并使用标准端点方法非常简化的 web.config。目前,此 wcf 服务在 iis 7.5 上作为默认网站的应用程序运行。如果可能的话,我需要它支持 http 和 https 接口。如果太复杂那就只有 https 了。维持当前方法如何最好地处理?
global.asax.cs 和 web.config 文件的内容非常基本:
Global.asax.cs 文件:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "ippay" string below
RouteTable.Routes.Add(new ServiceRoute("myservice", new WebServiceHostFactory(),
typeof(myservice)));
}
}
Web.config 文件:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" contentTypeMapper="myservice.Util.RawMapper,myservice">
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
我找到了答案:您只需将此代码片段放入 web.config 的 serviceModel 标记中即可:
<bindings>
<webHttpBinding>
<binding>
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
感谢这篇文章:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/1dd991a1-e32f-4035-a406-994729858b40
我的完整 web.config 是这样的:
<?xml version="1.0"?> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" /> </system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules> </system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<bindings>
<webHttpBinding>
<binding>
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
<security mode="Transport" >
</security>
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints> </system.serviceModel> </configuration>
如果您不想同时使用 HTTP 和 HTTPS,则上述方法有效。就我而言,我两者都需要,因为有些服务需要 SSL(身份验证),而另一些则不需要,因为它们提供的信息不敏感。身份验证服务实现进行验证,如果请求不是来自 https 方案,则拒绝回答。
如果您想在同一端点上设置 HTTP 和 HTTPS,则可以使用以下配置。
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding>
<security mode="Transport" />
</binding>
<binding name="UnsecureBinding"></binding>
</webHttpBinding>
</bindings>
<protocolMapping>
<add scheme="http" binding="webHttpBinding" bindingConfiguration="UnsecureBinding" />
</protocolMapping>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>