我使用以下方式创建组织服务代理对象:
[ThreadStatic]
public static OrganizationServiceProxy OrgServiceProxy;
// ...
sLog.DebugFormat("Get AuthenticationProviderType...");
AuthenticationProviderType _crmAuthType = this.GetServerType(parameters.DiscoveryUri);
sLog.DebugFormat("Get AuthenticationProviderType - DONE!");
// ...
sLog.Info("Perform metadata download (ServiceConfigurationFactory.CreateConfiguration)...");
IServiceConfiguration<IOrganizationService> _crmServiceConfiguration = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(parameters.OrgServiceUri);
sLog.Info("Perform metadata download (ServiceConfigurationFactory.CreateConfiguration) - DONE");
// ...
// enable proxy types
var behavior = new ProxyTypesBehavior() as IEndpointBehavior;
behavior.ApplyClientBehavior(_crmServiceConfiguration.CurrentServiceEndpoint, null);
// ...
public OrganizationServiceProxy GetServiceProxy(ICRMConnectionParameters parameters)
{
// ...
ClientCredentials clientCreds = new ClientCredentials();
clientCreds.Windows.ClientCredential.UserName = parameters.UserName;
clientCreds.Windows.ClientCredential.Password = parameters.Password;
clientCreds.Windows.ClientCredential.Domain = parameters.Domain;
sLog.DebugFormat("Setup client proxy...");
OrgServiceProxy = new OrganizationServiceProxy(_crmServiceConfiguration, clientCreds);
sLog.DebugFormat("Setup client proxy - DONE.");
return OrgServiceProxy;
}
请注意,AuthenticationProviderType和IServiceConfiguration是静态缓存的。上面的代码是名为CRMConnection的类的一部分。
我还有一个抽象类(ProxyUser),它包含以下属性:
private CRMConnection conn;
// ...
protected OrganizationServiceProxy OrgServiceProxy
{
get
{
//return orgService;
return this.Conn.GetServiceProxy();
}
}
protected CRMConnection Conn
{
get
{
conn = conn ?? new CRMConnection();
return conn;
}
}
在另一个继承ProxyUser的类中,我有以下代码的方法:
ColumnSet columnSet = new ColumnSet();
ConditionExpression condition1 = new ConditionExpression("new_id", ConditionOperator.NotNull);
FilterExpression filter = new FilterExpression(LogicalOperator.And);
filter.AddCondition(condition1);
QueryExpression query = new QueryExpression()
{
EntityName = new_brand.EntityLogicalName,
ColumnSet = columnSet,
Criteria = filter,
NoLock = true
};
EntityCollection res = OrgServiceProxy.RetrieveMultiple(query);
现在我们来到这一点:)
如果我设置了正确的参数 - 组织服务网址,发现服务网址,用户名,密码和域,一切都按预期工作。但是,如果设置了错误的密码,在下面的行中,服务就是没有响应。它没有发生任何事情。
EntityCollection res = OrgServiceProxy.RetrieveMultiple(query);
当然,我期待身份验证失败错误。我在这里缺少什么建议?
提前致谢!
答案 0 :(得分:0)
我在 GetServiceProxy 方法中添加以下行解决了这个问题 - 当创建 ClientCredentials 时:
clientCreds.SupportInteractive = false;
我在控制台应用程序中移动整个逻辑后想出来了。当设置了错误的密码并且应用程序处于调试模式时,我将获得Windows登录提示。然后我找到了this answer。