尝试将ASP.NET CORE 2 Web API应用程序连接到WCF服务

时间:2019-03-26 16:54:14

标签: .net wcf asp.net-core asp.net-core-webapi

我昨天开始了一个POC项目,以连接到现有的WCF服务,并且我正在使用.Net Core 2.1 Web API应用程序和Swagger发布测试请求消息。我将BasicHttpBinding和Transport用于BasicHttpSecurityMode。

在发送测试请求消息时,我收到以下异常:

“ HTTP请求未经客户端身份验证方案'Anonymous'的授权。从服务器收到的身份验证标头是'Basic Realm'。”

然后我使用.NET Framework 4.6.1创建了一个Microsoft Web API项目,以调用WCF服务,并且能够获得状态码200 HTTP响应。

在我看来,这似乎是.NET Core 2 Web API项目连接到WCF服务的问题。我已经做过一些研究,看来这种设计架构绝对应该可行。

Microsoft甚至已经开发了一个提供程序工具来完成此操作,这是其提供程序工具上的MS文章链接:https://docs.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide

我什至试图让我的.NET Core Web API控制器调用在.NET 4.6.1类库项目中构建的处理程序类都无济于事,但我仍然得到“ HTTP请求未经客户端身份验证方案的授权” “基本”。从服务器收到的身份验证标头是“基本领域”。”例外。

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IInsuranceService))
            {
                System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.AllowCookies = true;
                result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
                return result;
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }

        private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IInsuranceService))
            {
                return new System.ServiceModel.EndpointAddress("https://myservicebaseURL\myservice.svc");
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }

1 个答案:

答案 0 :(得分:0)

感谢大家的投入。我可以按照article中详细介绍的步骤解决问题。

请阅读以下有关我的更新代码,以从我的.NET Core 2 Web Api调用WCF服务:

 public async Task<DataFetchServiceResponseUsingPatient> FetchEntity(String ID)
    {
      var userName = _authenticationSettings.Value.UserName;
      var password = _authenticationSettings.Value.Password;
      var url = _authenticationSettings.Value.Url;

      var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);

      basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
      ChannelFactory<IMyService> factory = null;
      factory = new ChannelFactory<IMyService>(basicHttpBinding, new EndpointAddress(new Uri(url)));
      factory.Credentials.UserName.UserName = userName;
      factory.Credentials.UserName.Password = password;
      var serviceProxy = factory.CreateChannel();
      ((ICommunicationObject)serviceProxy).Open();
      var opContext = new OperationContext((IClientChannel)serviceProxy);
      var prevOpContext = OperationContext.Current;
      OperationContext.Current = opContext;

      var result = await serviceProxy.MyWCFServiceMethod(ID);

      //Cleanup Factory Objects
      factory.Close();
      ((ICommunicationObject)serviceProxy).Close();
      return result;    
    }