在BizTalk中使用C#Restharp方法,引发未设置为对象实例的NullReferenceException&Quot;对象引用 [英] Using C# Restharp Method in BizTalk, throws NullReferenceException "Object reference not set to an instance of an object"

查看:0
本文介绍了在BizTalk中使用C#Restharp方法,引发未设置为对象实例的NullReferenceException&Quot;对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    public static string getAccessToken()
    {
       
            var client = new RestClient("...");
            client.Timeout = -1;
            var request = new RestRequest() { Method = Method.POST };
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", "...");
            request.AddParameter("client_secret", "...");
            IRestResponse token = client.Execute(request);
            string varToken = token.Content;
          
            return varToken;
        
    }

此获取令牌的方法有效! 但使用令牌的方法抛出未设置为对象实例的NullReferenceException&Quot;对象引用

    public static string testAccessToken(string token)
    {
       
       var client = new RestClient("...");
       client.Timeout = -1;
       var request = new RestRequest(Method.GET);
       request.AddHeader("Content-Type", "application/json");
       request.AddHeader("Authorization", "Bearer " + token);

       IRestResponse response = client.Execute(request);
       string varToken = response.Content;

       
       return varToken;
       
        
    }

当我在Visual Studio窗体应用程序中测试方法testAccessToken(字符串令牌)时,它可以工作,但当我使用它时,BizTalk它抛出一个未设置为对象实例的NullReferenceException&Quot;对象引用

有人知道如何解决这个问题吗?

推荐答案

问题已解决。 即使在示例Windows窗体应用程序中创建的RestSharp类返回结果,BizTalk中的同一个类也不返回任何内容。 原因是TLS加密。许多服务器仍然允许TLS 1.0到1.2,但OAuth2环境中的服务器明确允许1.2,包括用于Business Central的Microsoft服务器。

通向目标的道路有两条。 首先,将TLS 1.2设置为Windows的默认协议。为此,需要手动更改注册表。 但是,客户服务器上的这些更改可能会导致客户的其他应用程序无法再进行通信。

更好的方法是在每次调用时告诉RestSharp应该使用哪种加密。 ";System.Net.ServicePointManager.SecurityProtocol=System.Net.SecurityProtocolType.Tls12";

瞧,BizTalk终于从类中得到了结果。

这篇关于在BizTalk中使用C#Restharp方法,引发未设置为对象实例的NullReferenceException&Quot;对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆