CRM Dynamics IOrganizationService SOAP Execute 使用最新的 SDK 失败
CRM Dynamics IOrganizationService SOAP Execute fails with the latest SDK
我一直在使用最新的Xrm sdk 来访问我公司的dynamics 365 帐户的元数据。这已经运行了一段时间,但是大约 3 周前它突然坏了。没有代码更改。这只意味着他们这边发生了一些变化。
下面是使用过的代码,非常简单。到目前为止,我得到的唯一信息是他们的安全协议已更改为 Tls12,因此尝试在创建组织服务对象之前设置该信息,就像这样
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
但这也没有影响。
var sc = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri("soap-url-from-365-developer-resources"));
var cc = new ClientCredentials();
cc.UserName.UserName = "username";
cc.UserName.Password = "password";
var organizationServiceProxy = new OrganizationServiceProxy(sc, cc);
organizationServiceProxy.EnableProxyTypes();
RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.Attributes,
RetrieveAsIfPublished = true
};
try
{
using (var organizationServiceContext = new CrmOrganizationServiceContext(organizationServiceProxy))
{
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)organizationServiceContext.Execute(request);
var entities = response.EntityMetadata
.Where(x => x.DisplayName.UserLocalizedLabel != null &&
!string.IsNullOrWhiteSpace(x.DisplayName.UserLocalizedLabel.Label)
)
.ToList();
}
}
catch (Exception ex)
{
Console.WriteLine("GetAllEntities {0}", ex.Message); }
执行时报错如下
System.InvalidOperationException: Metadata contains a reference that cannot be resolved: 'https://macfar.crm4.dynamics.com/XRMServices/2011/Organization.svc?wsdl&sdkversion=9'. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Description.MetadataExchangeClient.MetadataLocationRetriever.DownloadMetadata(TimeoutHelper timeoutHelper)
at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
--- End of inner exception stack trace ---
at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState)
at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(MetadataRetriever retriever)
at Microsoft.Xrm.Sdk.Client.ServiceMetadataUtility.RetrieveServiceEndpointMetadata(Type contractType, Uri serviceUri, Boolean checkForSecondary)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1..ctor(Uri serviceUri, Boolean checkForSecondary)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri serviceUri, Boolean enableProxyTypes, Assembly assembly)
at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration[TService](Uri serviceUri, Boolean enableProxyTypes, Assembly assembly)
at Microsoft.Xrm.Sdk.Client.ServiceProxy`1..ctor(Uri uri, Uri homeRealmUri, ClientCredentials clientCredentials, ClientCredentials deviceCredentials)
我已经尝试了谷歌搜索此错误消息的所有方法,但是都是徒劳的。因此,非常感谢任何解决此问题的帮助
去年 SDK 兼容性发生了相当多的变化。
您需要确保您的 SDK 版本与您的 .Net Framework 和 CRM 版本相匹配。
Dynamics 365 SDK Backwards Compatibility
With the deprecation of Azure Access Control Service (ACS), we have to
modify our SDK authentication code by removing all references to ACS.
Effective from versions Microsoft Dynamics CRM Online 2016 Update 1
(v8.1.1) and Microsoft Dynamics 365 (v8.2), we removed Live ID support
and ACS dependencies on the server-side.
We also removed Microsoft.Xrm.Client from the CRM 2016 (8.x) SDK
client because it was not compliant with the OAuth changes, and
replaced it with Microsoft.Xrm.Tooling.Connector. You can use the
current Microsoft Dynamics 365 Software Development Kit (SDK) to
access Microsoft Dynamics CRM back to version 6.x for both auth and
messaging.
When upgrading to Dynamics 365, make sure you use the latest Microsoft
Dynamics 365 Software Development Kit (SDK). The following outlines
the current supported matrix for other SDK clients:
我一直在使用最新的Xrm sdk 来访问我公司的dynamics 365 帐户的元数据。这已经运行了一段时间,但是大约 3 周前它突然坏了。没有代码更改。这只意味着他们这边发生了一些变化。
下面是使用过的代码,非常简单。到目前为止,我得到的唯一信息是他们的安全协议已更改为 Tls12,因此尝试在创建组织服务对象之前设置该信息,就像这样
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
但这也没有影响。
var sc = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri("soap-url-from-365-developer-resources"));
var cc = new ClientCredentials();
cc.UserName.UserName = "username";
cc.UserName.Password = "password";
var organizationServiceProxy = new OrganizationServiceProxy(sc, cc);
organizationServiceProxy.EnableProxyTypes();
RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.Attributes,
RetrieveAsIfPublished = true
};
try
{
using (var organizationServiceContext = new CrmOrganizationServiceContext(organizationServiceProxy))
{
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)organizationServiceContext.Execute(request);
var entities = response.EntityMetadata
.Where(x => x.DisplayName.UserLocalizedLabel != null &&
!string.IsNullOrWhiteSpace(x.DisplayName.UserLocalizedLabel.Label)
)
.ToList();
}
}
catch (Exception ex)
{
Console.WriteLine("GetAllEntities {0}", ex.Message); }
执行时报错如下
System.InvalidOperationException: Metadata contains a reference that cannot be resolved: 'https://macfar.crm4.dynamics.com/XRMServices/2011/Organization.svc?wsdl&sdkversion=9'. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Description.MetadataExchangeClient.MetadataLocationRetriever.DownloadMetadata(TimeoutHelper timeoutHelper)
at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
--- End of inner exception stack trace ---
at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState)
at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(MetadataRetriever retriever)
at Microsoft.Xrm.Sdk.Client.ServiceMetadataUtility.RetrieveServiceEndpointMetadata(Type contractType, Uri serviceUri, Boolean checkForSecondary)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1..ctor(Uri serviceUri, Boolean checkForSecondary)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri serviceUri, Boolean enableProxyTypes, Assembly assembly)
at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration[TService](Uri serviceUri, Boolean enableProxyTypes, Assembly assembly)
at Microsoft.Xrm.Sdk.Client.ServiceProxy`1..ctor(Uri uri, Uri homeRealmUri, ClientCredentials clientCredentials, ClientCredentials deviceCredentials)
我已经尝试了谷歌搜索此错误消息的所有方法,但是都是徒劳的。因此,非常感谢任何解决此问题的帮助
去年 SDK 兼容性发生了相当多的变化。
您需要确保您的 SDK 版本与您的 .Net Framework 和 CRM 版本相匹配。
Dynamics 365 SDK Backwards Compatibility
With the deprecation of Azure Access Control Service (ACS), we have to modify our SDK authentication code by removing all references to ACS. Effective from versions Microsoft Dynamics CRM Online 2016 Update 1 (v8.1.1) and Microsoft Dynamics 365 (v8.2), we removed Live ID support and ACS dependencies on the server-side.
We also removed Microsoft.Xrm.Client from the CRM 2016 (8.x) SDK client because it was not compliant with the OAuth changes, and replaced it with Microsoft.Xrm.Tooling.Connector. You can use the current Microsoft Dynamics 365 Software Development Kit (SDK) to access Microsoft Dynamics CRM back to version 6.x for both auth and messaging.
When upgrading to Dynamics 365, make sure you use the latest Microsoft Dynamics 365 Software Development Kit (SDK). The following outlines the current supported matrix for other SDK clients: