c# - 从控制台应用程序调用动态 CRM Web 服务时如何指定代理详细信息
问题描述
我是 Dynamics CRM Web 服务的新手,我想从控制台应用程序使用 Web 服务,它在我的本地工作,但是当它进入生产服务器时它不工作,因为生产服务器需要在调用 Web 服务之前设置代理. 有人可以在调用 Web 服务之前解释如何使用代理吗,这是在我的本地机器上运行的代码
private static void ConnectToDynamics()
{
string odataUrl = _dynamicsURL;
string appId = Id;
string clientSecret = _secret;
AuthenticationParameters authArg = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(odataUrl)).Result;
AuthenticationContext authCtx = new AuthenticationContext(authArg.Authority);
AuthenticationResult authRes = authCtx.AcquireTokenAsync(authArg.Resource, new ClientCredential(appId, clientSecret)).Result;
using (OrganizationWebProxyClient webProxyClient = new OrganizationWebProxyClient(new Uri(odataUrl), false))
{
webProxyClient.HeaderToken = authRes.AccessToken;
using (OrganizationServiceContext ctx = new OrganizationServiceContext((IOrganizationService)webProxyClient))
{
var accounts = (from i in ctx.CreateQuery("entity") orderby i["name"] select i).Take(100);
foreach (var account in accounts)
Console.WriteLine(account["name"]);
}
}
}
解决方案
下面是示例代码HttpWebResponse
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
WebProxy proxy = new WebProxy("proxy.xyz.local", 81) { UseDefaultCredentials = true };
WebRequest request = WebRequest.Create(globaConfigStatciValues.Url);
request.Proxy = proxy;
request.Method = "GET";
request.Credentials = new NetworkCredential(globaConfigStatciValues.userName,
globaConfigStatciValues.Password);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
isConnectionSucessfull = true;
tracing.Trace($" Resposne is correct {response.StatusCode}");
TracingMessage += $"Resposne is correct {response.StatusCode} \n";
}
else
{
TracingMessage += $"Response from connecting to API {response.StatusCode} \n";
tracing.Trace($"Response from connecting to API {response.StatusCode}");
}
}
catch (Exception e)
{
TracingMessage += $" In catch block {e} \n";
tracing.Trace($" In catch block {e}");
createLogRecord( e.StackTrace,TracingMessage);
// throw new Exception($"There was an issue with connecting to API {e.Message}");
}
}
推荐阅读
- python - Pandas 中的行系列与列系列
- javascript - 基于概率和百分比的返回值
- javascript - Passport Google Oauth 在登录时不会重定向到成功的路由
- javascript - PubNub React-Native JS SDK 版本 2 有状态组件
- javascript - 我正在使用“帆”:“~1.2.4”。我正在尝试连接到 MongoDb。但是当我尝试扬帆起航时,我收到了这个错误:
- dynamics-crm-online - RESCO:在使用 JSBridge 更改表单上的字段时在查找字段上动态设置查找视图
- python - Colab 错误:ModuleNotFoundError:没有名为“geopandas”的模块
- c - 如何在 C 中改进这个链表以使其没有无限循环?
- javascript - RESCO OfflineHtml:无法调试 Javascript 代码
- flutter - 在颤动中使用 AlertDialog 从数据库中显示选定的数据