我遇到了同样的错误,Damien_The_Unbeliever 的回答帮助我理解 && 解决了我的问题:“但是,在运行时,只使用了主项目的 app.config”。
我在一个类库项目中有我的 wsdl 代理。尽管我的 UnitTest 项目能够从该类库的 app.config 中读取,但在运行时该类库的 app.config 变得无关紧要。
我的解决方案是为自动生成的客户端类创建一个部分类,并依赖从代码配置的 System.ServiceModel,而不是从谁知道 app.config 中读取它:
public partial class WsdlClient
{
public WsdlClient (string endpointUrl, TimeSpan timeout, string username, string password) :
base(WsdlClient.GetBindingForEndpoint(timeout), WsdlClient.GetEndpointAddress(endpointUrl))
{
this.ChannelFactory.Credentials.UserName.UserName = username;
this.ChannelFactory.Credentials.UserName.Password = password;
}
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(TimeSpan timeout)
{
var httpsBinding = new BasicHttpsBinding();
httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;
var integerMaxValue = int.MaxValue;
httpsBinding.MaxBufferSize = integerMaxValue;
httpsBinding.MaxReceivedMessageSize = integerMaxValue;
httpsBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
httpsBinding.AllowCookies = true;
httpsBinding.ReceiveTimeout = timeout;
httpsBinding.SendTimeout = timeout;
httpsBinding.OpenTimeout = timeout;
httpsBinding.CloseTimeout = timeout;
return httpsBinding;
}
private static System.ServiceModel.EndpointAddress GetEndpointAddress(string endpointUrl)
{
if (!endpointUrl.StartsWith("https://"))
{
throw new UriFormatException("The endpoint URL must start with https://.");
}
return new System.ServiceModel.EndpointAddress(endpointUrl);
}
}
WsdlClient MyWsdlClient =>
new WsdlClient (ConfigurationManager.AppSettings["endpointUrl"]
, new TimeSpan(0, 0, 1, 0),
"blabla",
"blabla");