【问题标题】:WCF Service unable to call from .NET CoreWCF 服务无法从 .NET Core 调用
【发布时间】:2023-03-05 07:04:01
【问题描述】:

我做错了,我想不通...我制作了 .NET Framework 4 控制台应用程序来与 SOAP 服务进行通信,使用 Topshelf 我在服务器上部署了服务,并通过简单的 URL 访问方法或使用 Boomerang 工具,我可以看到服务正在返回值

网址:http://35.231.17.237:8066/ERPCommunicationService/OriginalService/IsServiceHealthy

但是现在,当我尝试从 .NET Core 项目访问相同的服务时,我不断收到错误消息:

System.ServiceModel.ProtocolException: 
     The remote server returned an unexpected response: (405) Method Not Allowed.
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(
      SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(
      String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.
      <CreateGenericTask>b__0(IAsyncResult asyncResult)
--- End of stack trace from previous location where exception was thrown ---

代码很简单,我成功使用服务端点将它连接到.NET Core项目,在那里我可以看到Reference.cs自动生成的文件和来自服务的所有方法......

这是来自客户端(.net 核心)的服务调用:

public async Task<bool> IsServiceHealthy()
{
   try
   {

      string servicesUrl = $"{_iConfiguration["servicesUrl"]}/IsServiceHealthy";


      //My binding setup, since ASP.NET Core apps don't use a web.config file
      var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
      binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
      binding.MaxReceivedMessageSize = 10485760;
      binding.SendTimeout = new TimeSpan(0, 0, 0, 180);
      binding.ReceiveTimeout = new TimeSpan(0, 0, 0, 180);


      var rsExec = new OriginalService.OriginalServiceClient(binding, 
                                        new EndpointAddress(servicesUrl));

      var clientFactory = rsExec.ChannelFactory.CreateChannel();
      var response = await clientFactory.IsServiceHealthyAsync();

      return response;
    }
    catch (Exception ex)
    {
        logging.LogError(ex.ToString());
        throw ex;
    }
}

以及来自服务器端的代码(.NET Framework 4):

界面:

    [OperationContract]
    [WebInvoke(Method = "GET",
        RequestFormat = WebMessageFormat.Json,
        UriTemplate = "/IsServiceHealthy")]
    bool IsServiceHealthy();

实施:

public bool IsServiceHealthy()
{
    bool serviceResult = false;

    byte[] test = new byte[200];
    var client = new ChannelFactory<BisWebWS.BisWebWSSOAPPortType>("BisWebWSSOAPPort")
    .CreateChannel();

    BisWebWS.tauthStrct auth = ServisBasic.GetAuth();

    try
    {
        var result = client.wsTest(new BisWebWS.wsTestRequest(test));

        serviceResult = result.wsTestResult;
    }
    catch (Exception ex)
    {
        logger.LogError(ex.InnerException.ToString());
    }

    return serviceResult;
}

每当我在谷歌上显示错误时,到处都是服务器端设置,但我有点卡住了,因为我安装了所有的东西......我使用的是 MS Windows Server 2012 R2 Datacenter,

感谢您分享如何解决此问题的想法

【问题讨论】:

    标签: wcf .net-core .net-4.0


    【解决方案1】:

    我们使用代理类调用服务的方式是一个Http Post请求,而方法上有一个GET修饰。它需要一个 Http Get 请求而不是 Post 请求。这可能会直接导致问题。

    [OperationContract]
        [WebInvoke(Method = "GET",
            RequestFormat = WebMessageFormat.Json,
            UriTemplate = "/IsServiceHealthy")]
        bool IsServiceHealthy();
    

    如果服务器使用Webhttpbinding托管服务,我们可以直接在浏览器地址栏中输入服务地址来获取结果,因为默认请求是Http Get请求(您的链接不可用)。
    这种服务称为Restful 式服务。
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-web-http-programming-model
    https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design
    典型的调用是使用 HttpClient 库构造一个带有请求体的 HTTP 请求。
    我们也可以使用客户端代理类来调用服务,就像你所做的那样。但是,我们应该保持服务器和客户端之间的绑定配置一致。
    WCF: There was no endpoint listening at, that could accept the message
    使用客户端代理类调用服务太复杂,调用Restful风格服务时最好使用HttpClient发送Http请求。
    此外,我们还可以使用 BasicHttpBinding 来托管服务,这样可以简化调用。无需添加 webhttpendpoint 行为,也无需添加额外的 [Webget] 装饰。
    简单来说,在使用客户端代理时,我们应该保持服务端和客户端的绑定一致。
    如果问题仍然存在,请随时告诉我。

    【讨论】:

    • 感谢您的回答,一旦我们对其进行测试,我会通知您是否有效并将其标记为已接受的答案。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多