Web services 连接到托管的CRM online 2011时发生Soap错误
很长一段时间以来,我一直在尝试解决这个错误。 “验证邮件安全性时出错” 我做了一些研究,人们说这是因为服务器和客户端之间的时间差。 这和其他人有同样的问题吗? 下面是我的错误的细节Web services 连接到托管的CRM online 2011时发生Soap错误,web-services,soap,dynamics-crm,dynamics-crm-2011,Web Services,Soap,Dynamics Crm,Dynamics Crm 2011,很长一段时间以来,我一直在尝试解决这个错误。 “验证邮件安全性时出错” 我做了一些研究,人们说这是因为服务器和客户端之间的时间差。 这和其他人有同样的问题吗? 下面是我的错误的细节 System.ServiceModel.FaultException was caught Message=An error occurred when verifying security for the message. Source=mscorlib StackTrace: Serv
System.ServiceModel.FaultException was caught
Message=An error occurred when verifying security for the message.
Source=mscorlib
StackTrace:
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at WindowsFormsApplication1.CrmSdk.Discovery.IDiscoveryService.Execute(DiscoveryRequest request)
at WindowsFormsApplication1.CrmSdk.Discovery.DiscoveryServiceClient.Execute(DiscoveryRequest request) in WindowsFormsApplication1\Service References\CrmSdk.Discovery\Reference.cs:line 723
at WindowsFormsApplication1.Form1.DiscoverOrganizationUrl(String organizationName, String discoveryServiceUrl) in Form1.cs:line 110
InnerException:
下面是我用来访问托管的CRM online Web服务的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
//using System.ServiceModel;
//using System.ServiceModel.Description;
namespace WindowsFormsApplication1
{
using CrmSdk;
using CrmSdk.Discovery;
using System.Net;
using System.Globalization;
using LocalServices;
using System.ServiceModel;
using System.Web.Services.Protocols;
public partial class Form1 : Form
{
///hosted CRM online
private const string DiscoveryServiceUrl = "https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc";
private IOrganizationService _service;
private string fetchXML =
@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='ownerid' operator='eq-userid' />
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'>
<attribute name='emailaddress1' />
</link-entity>
</entity>
</fetch>
";
public Form1()
{
InitializeComponent();
//GetDataFromCRM();
GetDataFromCRM2();
}
private void GetDataFromCRM2()
{
private string DiscoverOrganizationUrl(string organizationName, string discoveryServiceUrl)
{
using (CrmSdk.Discovery.DiscoveryServiceClient client = new CrmSdk.Discovery.DiscoveryServiceClient("CustomBinding_IDiscoveryService", discoveryServiceUrl))
{
//ApplyCredentials(client, credentials);
client.ClientCredentials.Windows.ClientCredential.UserName = UserName;
client.ClientCredentials.Windows.ClientCredential.Password = Password
client.ClientCredentials.Windows.ClientCredential.Domain = Domain
CrmSdk.Discovery.RetrieveOrganizationRequest request = new CrmSdk.Discovery.RetrieveOrganizationRequest()
{
UniqueName = organizationName
};
try
{
CrmSdk.Discovery.RetrieveOrganizationResponse response = (CrmSdk.Discovery.RetrieveOrganizationResponse)client.Execute(request);
foreach (KeyValuePair<CrmSdk.Discovery.EndpointType, string> endpoint in response.Detail.Endpoints)
{
if (CrmSdk.Discovery.EndpointType.OrganizationService == endpoint.Key)
{
Console.WriteLine("Organization Service URL: {0}", endpoint.Value);
return endpoint.Value;
}
}
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"Organization {0} does not have an OrganizationService endpoint defined.", organizationName));
}
catch (FaultException e)
{
MessageBox.Show(e.Message);
throw;
}
catch (SoapHeaderException e)
{
MessageBox.Show(e.Message);
throw;
}
catch (SoapException e)
{
MessageBox.Show(e.Message);
throw;
}
return null;
}
}
//private static void ApplyCredentials<TChannel>(ClientBase<TChannel> client, ICredentials credentials)
// where TChannel : class
//{
// client.ClientCredentials.Windows.ClientCredential = credentials.Windows.ClientCredential;
//}
private void ExecuteFetch(string serviceUrl)
{
using (OrganizationServiceClient client = new OrganizationServiceClient("CustomBinding_IOrganizationService", new EndpointAddress(serviceUrl)))
{
client.ClientCredentials.Windows.ClientCredential.UserName = UserName;
client.ClientCredentials.Windows.ClientCredential.Password = Password;
client.ClientCredentials.Windows.ClientCredential.Domain = Domain;
_service = (IOrganizationService)client;
FetchExpression expression = new FetchExpression();
expression.Query =
@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='ownerid' operator='eq-userid' />
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'>
<attribute name='emailaddress1' />
</link-entity>
</entity>
</fetch>
";
EntityCollection result = _service.RetrieveMultiple(expression);
DataTable temp = ConvertEntityToTable(result);
}
}
/// Convert Entity To datatable
private DataTable ConvertEntityToTable(EntityCollection result)
{
DataTable dt = new DataTable();
int rowCount = result.Entities.Count();
try
{
for (int i = 0; i < rowCount; i++)
{
DataRow dr = dt.NewRow();
Entity currentEntity = (Entity)result.Entities[i];
var keys = currentEntity.Attributes.Count();
for (int j = 0; j < keys; j++)
{
string columName = currentEntity.Attributes[j].Key;
string value = currentEntity.Attributes[j].Value.ToString();
if (dt.Columns.IndexOf(columName) == -1)
dt.Columns.Add(columName, Type.GetType("Sysem.String"));
dr[columName] = value;
}
dt.Rows.Add(dr);
}
return dt;
}
catch (Exception exp)
{
throw;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Web;
//使用System.ServiceModel;
//使用System.ServiceModel.Description;
命名空间Windows窗体应用程序1
{
使用CrmSdk;
使用CrmSdk.Discovery;
Net系统;
利用制度全球化;
使用本地服务;
使用System.ServiceModel;
使用System.Web.Services.Protocols;
公共部分类Form1:Form
{
///托管在线CRM
私有常量字符串DiscoveryServiceUrl=”https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc";
私人组织服务(简称"服务"),;
私有字符串获取XML=
@"
";
公共表格1()
{
初始化组件();
//GetDataFromCRM();
GetDataFromCRM2();
}
私有void GetDataFromCRM2()
{
私有字符串DiscoverOrganizationUrl(字符串organizationName、字符串discoveryServiceUrl)
{
使用(CrmSdk.DiscoveryServiceClient=new CrmSdk.DiscoveryServiceClient.DiscoveryServiceClient(“CustomBinding_IDiscoveryService”,discoveryServiceUrl))
{
//ApplyCredentials(客户、凭证);
client.ClientCredentials.Windows.ClientCredential.UserName=用户名;
client.ClientCredentials.Windows.ClientCredential.Password=密码
client.ClientCredentials.Windows.ClientCredential.Domain=域
CrmSdk.Discovery.RetrieveOrganizationRequest=新的CrmSdk.Discovery.RetrieveOrganizationRequest()
{
UniqueName=组织名称
};
尝试
{
CrmSdk.Discovery.RetrieveOrganizationResponse=(CrmSdk.Discovery.RetrieveOrganizationResponse)client.Execute(请求);
foreach(响应中的KeyValuePair端点.Detail.Endpoints)
{
if(CrmSdk.Discovery.EndpointType.OrganizationService==endpoint.Key)
{
WriteLine(“组织服务URL:{0}”,endpoint.Value);
返回endpoint.Value;
}
}
抛出新的InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
“组织{0}未定义OrganizationService终结点。”,organizationName));
}
捕获(错误异常e)
{
MessageBox.Show(e.Message);
投掷;
}
捕获(SoapHeaderException e)
{
MessageBox.Show(e.Message);
投掷;
}
捕获(SOAPE例外)
{
MessageBox.Show(e.Message);
投掷;
}
返回null;
}
}
//私有静态void ApplyCredentials(ClientBase客户端、ICredentials凭据)
//TChannel:class在哪里
//{
//client.ClientCredentials.Windows.ClientCredential=credentials.Windows.ClientCredential;
//}
私有void ExecuteFetch(字符串服务URL)
{
使用(OrganizationServiceClient客户端=新的OrganizationServiceClient(“CustomBinding_iorOrganizationService”,新的端点地址(serviceUrl)))
{
client.ClientCredentials.Windows.ClientCredential.UserName=用户名;
client.ClientCredentials.Windows.ClientCredential.Password=密码;
client.ClientCredentials.Windows.ClientCredential.Domain=域;
_服务=(IOR组织服务)客户;
FetchExpression=新的FetchExpression();
表达式。查询=
@"
<bindings>
<customBinding>
<binding name="CustomBinding_IDiscoveryService">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
messageVersion="Default" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
<httpsTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy="true" requireClientCertificate="false"
/>
</binding>