从dotnet core 2.1迁移到3.1后使用ChannelFactory使用WCF的问题

时间:2020-10-09 16:03:10

标签: c# .net asp.net-core-3.1 .net-core-3.1

我在调用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;

0 个答案:

没有答案