我目前正在使用 CRM 2015 SDK。我只是尝试使用此 SDK 更新 C# 中的值。但由于我试图弄清楚的某些原因,当我保存我的
context
时遇到了麻烦。
有代码:
foreach (KeyValuePair<string, Account> account in dicAccount)
{
//Calcul of url/login/date/key/customer values
string generatedUrl = Utilities.GenerateURL(url, login, date, key, customer);
account.Value.new_Link = generatedUrl;
if (!context.IsAttached(account.Value))
{
context.Attach(account.Value);
}
context.UpdateObject(account.Value);
}
SaveChangesResultCollection results = context.SaveChanges(SaveChangesOptions.ContinueOnError);
if (results != null)
{
foreach (SaveChangesResult result in results)
{
Type type = result.Request.GetType();
bool hasError = result.Error != null;
Entity entity = (Entity)result.Request.Parameters["Target"];
if (type == typeof(UpdateRequest))
{
if (hasError)
{
if (entity != null)
{
log.Error(result.Error.Message);
}
}
}
在我的 Dynamics 实体上,我有这个:
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("new_link")]
public string new_Link
{
get
{
return this.GetAttributeValue<string>("new_link");
}
set
{
this.OnPropertyChanging("new_link");
this.SetAttributeValue("new_link", value);
this.OnPropertyChanged("new_link");
}
}
现在,我收到 LogError 打印的错误:
格式化程序在尝试反序列化消息时引发异常:尝试反序列化参数时出错http://schemas.microsoft.com/xrm/2011/Contracts/Services:request。 InnerException 消息是“第 1 行位置 12271 中出现错误。元素“http://schemas.datacontract.org/2004/07/System.Collections.Generic:value”包含来自映射到名称“”的类型的数据。 http://schemas.microsoft.com/xrm/7.1/Contracts:ConcurrencyBehavior'。解串器不知道映射到该名称的任何类型。考虑更改 DataContractResolver 上 ResolveName 方法的实现,以返回名称“ConcurrencyBehavior”和命名空间“http://schemas.microsoft.com/xrm/7.1/Contracts”的非空值。请参阅 InnerException 了解更多详细信息。
经过几次搜索,我发现了两个可能的原因:
启用代理类型:事实是我有代码可以做到这一点。所以这对我没有帮助。
_serviceProxy.EnableProxyTypes();
SDK版本:我看到一些关于SDK版本7.0可能导致此问题的答案。事实上,我正在使用7.1版本,并且我也尝试使用最新的7.1.1。我使用这个DLL的:
Microsoft.Xrm.Client
,Microsoft.Xrm.Sdk
,Microsoft.Crm.Sdk.Proxy
此元素的类型:我还尝试使用基本字符串作为数据类型。仍然存在序列化的问题。
这些想法都不能解决我的问题,现在,我真的不知道我应该在哪里寻找解决这个问题的方法。
问题也可能是未知类型。在 OrganizationServiceProxy 上启用代理类型非常重要。它解决了我的问题并出现类似的错误
using (OrganizationServiceProxy proxy = new OrganizationServiceProxy(organizationUri, null, credentials, null))
{
proxy.EnableProxyTypes();
}
不是 100% 问题是什么,但我建议尝试以下方法看看是否有帮助。
重新生成您的代理,可能是您的代理已过期,这就是为什么
deserializer has no knowledge of any type that maps to this name
。尝试使用后期绑定来看看是否有效,如果早期绑定代码存在问题,可以帮助缩小范围。例如:
Entity account = new Entity("account");
account.Id = new Guid("");
account["new_link"] = "your value";
service.Update(account);
我已经通过更新参考解决了这个问题
Microsoft.Xrm.Tooling.Connector
事实证明,我使用的是旧版本,与其他 SDK 参考不匹配,但在构建程序时并没有崩溃。
您可以使用 NuGet 来获取该程序集。 这是该项目的网址: https://learn.microsoft.com/es-es/dotnet/api/microsoft.xrm.tooling.connector?view=dynamics-xrmtooling-ce-9
在 CRM Dynamics online 中,对我有帮助的是在插件项目的 AssemblyInfo.cs 文件中添加这一行: [程序集:Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()]