我有两个WCF RESTful服务 – “一般”服务是公开的,没有安全性;我打算使用基于SSL的基本身份验证的“admin”服务.这是我的服务器端web.config:
<system.serviceModel> <bindings> <webHttpBinding> <binding name="general" maxReceivedMessageSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" /> <security mode="None"> <transport clientCredentialType="None" /> </security> </binding> <binding name="admin" maxReceivedMessageSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" /> <security mode="Transport"> <transport clientCredentialType="Basic" /> </security> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <services> <service name="MyNamespace.AppServices.GeneralService"> <endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IGeneralService" behaviorConfiguration="web" bindingConfiguration="general" /> </service> <service name="MyNamespace.AppServices.AdminService"> <endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IAdminService" behaviorConfiguration="web" bindingConfiguration="admin" /> </service> </services> </system.serviceModel>
在客户端,我目前的代码如下:
private static IGeneralService GetGeneralChannel() { WebHttpBinding binding = new WebHttpBinding(); binding.Security.Mode = WebHttpSecurityMode.None; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; binding.MaxReceivedMessageSize = Int32.MaxValue; binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue; WebChannelFactory<IGeneralService> cf = new WebChannelFactory<IGeneralService>(binding,new Uri("http://localhost:1066/GeneralService")); IGeneralService channel = cf.CreateChannel(); return channel; } private static IAdminService GetAdminChannel() { WebHttpBinding binding = new WebHttpBinding(); binding.Security.Mode = WebHttpSecurityMode.Transport; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; binding.MaxReceivedMessageSize = Int32.MaxValue; binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue; WebChannelFactory<IAdminService> cf = new WebChannelFactory<IAdminService>(binding,new Uri("http://localhost:1066/AdminService")); cf.Credentials.UserName.UserName = "myUserName"; cf.Credentials.UserName.Password = "myPassword"; IAdminService channel = cf.CreateChannel(); return channel; }
问题是,由于我显然不想硬编码所有这些配置信息,我需要如何在客户端的web.config中提供它?对于我来说,绑定元素在客户端上看起来和服务器上看起来完全相同,这是非常明显的.但是,在哪里可以指定分配给WebChannelFactory的凭据?
任何帮助和/或洞察将不胜感激.
谢谢,
史蒂夫