如何告诉dot-netGraph SDK使用自定义代理



我在获取dotnet图sdk以按预期使用代理设置时遇到问题。我曾尝试在使用GraphClientFactory初始化HttpClient时设置代理,但似乎仍然出现了下面显示的异常。这些相同的设置正在传递给ConfidentialClientApplicationBuilder,并用于成功请求身份验证令牌。它还通过相同的代理设置与内部应用程序API进行通信,没有任何问题。有人能帮我指一下正确的方向吗?我使用的是Graph SDK 4.36.0.0的最新版本

public async Task Initialize(WebProxy webProxy)
{
var httpClientFactory = new GraphIntegrationHttpClientFactory(webProxy);
IConfidentialClientApplication daemonClient;
//Creates a daemon service.
daemonClient = ConfidentialClientApplicationBuilder.Create(_clientId)
.WithAuthority(string.Format(Constants.AuthorityFormat, _tenantId))
.WithClientSecret(_clientSecret)
.WithHttpClientFactory(httpClientFactory)
.Build();
//Gets auth token.
AuthenticationResult authResult = await daemonClient.AcquireTokenForClient(new string[] { Constants.MsGraphScope })
.ExecuteAsync();
ClientCredentialProvider authProvider = new ClientCredentialProvider(daemonClient);
var handlers = GraphClientFactory.CreateDefaultHandlers(authProvider);
handlers.Add(new LoggingHandler(_logService));
var httpClient = GraphClientFactory.Create(handlers, proxy: webProxy);
httpClient.Timeout = TimeSpan.FromMinutes(5);
_graphServiceClient = new GraphServiceClient(httpClient);
}

异常

Microsoft.Graph.ServiceException: Code: generalException
Message: An error occurred sending the request.
---> System.InvalidOperationException: When using a non-null Proxy, the WindowsProxyUsePolicy property must be set to WindowsProxyUsePolicy.UseCustomProxy.
at Application.Common.Graph.Handlers.LoggingHandler.<SendAsync>d__2.MoveNext() in C:UsersDamienOstlerDocumentsGitHubApplicationsrcmainApplication.Common.GraphHandlersLoggingHandler.cs:line 42
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.RedirectHandler.<SendAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.RetryHandler.<SendAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.CompressionHandler.<SendAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.AuthenticationHandler.<SendAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.SimpleHttpProvider.<SendRequestAsync>d__13.MoveNext()
--- End of inner exception stack trace ---
at Microsoft.Graph.SimpleHttpProvider.<SendRequestAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.SimpleHttpProvider.<SendAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__40.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.BaseRequest.<SendAsync>d__34`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.UserRequest.<GetAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Application.Common.Graph.OneDriveIntegration.<GetUsers>d__10.MoveNext() in C:UsersDamienOstlerDocumentsGitHubApplicationsrcmainApplication.Common.GraphOneDriveIntegration.cs:line 144

我找到了一种解决这个问题的方法,通过传递一个客户HttpClientHandler,将所有信息设置为GraphClientFactory Create方法的finalhandler参数。我在github页面上打开了一个关于传递代理无法正常工作的新问题。

最新更新