我在调用wcf-service时遇到问题,该服务在核心2.1中工作正常,但在迁移到核心3.1后失败。
我从3.1中调用wcf-service得到的异常消息如下:
以下是创建频道的方法:
public T CreateChannel<T>(ServiceClientRequest clientRequest, Binding binding) where T : class
{
if (clientRequest.Endpoint == string.Empty)
{
var errorMessage = $"Endpoint is missing for type {typeof(T).FullName}";
_logger.LogError(errorMessage);
throw new ArgumentNullException(errorMessage);
}
if (_factories.TryGetValue(clientRequest.Endpoint, out var factory) )
{
return ((ChannelFactory<T>)factory).CreateChannel();
}
var clientFactory = new ChannelFactory<T>(binding, new EndpointAddress(new Uri(clientRequest.Endpoint)));
clientFactory.Credentials.UserName.UserName = clientRequest.Username;
clientFactory.Credentials.UserName.Password = clientRequest.Password;
_factories.TryAdd(clientRequest.Endpoint, clientFactory);
return clientFactory.CreateChannel();
}
类型 T 是来自wcf服务的引用,即通过Visual Studio中的连接服务导入的数据合同/界面。
此外, BasicHttpBinding 给出为
var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;