EndpointNotFound 异常 - Dynamics CRM 2011
Posted
技术标签:
【中文标题】EndpointNotFound 异常 - Dynamics CRM 2011【英文标题】:EndpointNotFound Exception - Dynamics CRM 2011 【发布时间】:2013-07-08 10:07:41 【问题描述】:在 Dynamics CRM 2011 service
上调用 Execute
方法时,将 ImportSolutionRequest 对象作为参数传递,引发以下 EndpointNotFound 异常:
There was no endpoint listening at http://Server.com:5555/Organization/XRMServices/2011/Organization.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
InnerException 是 System.Net.WebException
:
"The remote server returned an error: (404) Not Found."
以下代码用于将解决方案导入 Dynamics CRM 2011 组织:
public override bool Execute()
try
Log.LogMessage(Properties.Resources.importSolutionStarted);
CustomNameSpace.Entities.Solution solution = new CustomNameSpace.Entities.Solution(new XrmConnection(DiscoveryServer, Port, Scheme, Organization, Domain, UserName, Password).Connection);
solution.ImportSolution(SolutionFilePath);
Log.LogMessage(Properties.Resources.importSolutionCompleted);
return true;
catch (ApplicationException exception)
Log.LogMessage(exception.Message);
return false;
这是解决方案类:
public partial class Solution
public Solution(CrmConnection crmConnection)
CrmConnection = crmConnection;
ProxyUri = new Uri(String.Format(CultureInfo.CurrentCulture, "0/XrmServices/2011/Organization.svc", CrmConnection.ServiceUri));
private Uri _proxyUri;
public Uri ProxyUri
get
return _proxyUri;
set
_proxyUri = value;
private CrmConnection _crmConnection;
public CrmConnection CrmConnection
get
return _crmConnection;
set
_crmConnection = value;
private IOrganizationService _crmService;
public IOrganizationService CrmService
get
return _crmService;
set
_crmService = value;
private OrganizationServiceProxy _crmProxy;
public OrganizationServiceProxy CrmProxy
get
return _crmProxy;
set
_crmProxy = value;
public Publisher CreatePublisher(string uniqueName, string friendlyName, Uri supportingWebsiteUrl, string customizationPrefix, string eMailAddress, string description)
try
Publisher crmSdkPublisher = new Publisher();
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
if (supportingWebsiteUrl != null)
crmSdkPublisher = new Publisher
UniqueName = uniqueName,
FriendlyName = friendlyName,
SupportingWebsiteUrl = supportingWebsiteUrl.AbsoluteUri,
CustomizationPrefix = customizationPrefix,
EMailAddress = eMailAddress,
Description = description
;
QueryExpression queryPublisher = new QueryExpression
EntityName = Publisher.EntityLogicalName,
ColumnSet = new ColumnSet("publisherid", "customizationprefix"),
Criteria = new FilterExpression()
;
queryPublisher.Criteria.AddCondition("uniquename", ConditionOperator.Equal, crmSdkPublisher.UniqueName);
EntityCollection queryPublisherResults;
queryPublisherResults = CrmService.RetrieveMultiple(queryPublisher);
Publisher SDKPublisherResults = null;
if (queryPublisherResults.Entities.Count > 0)
SDKPublisherResults = (Publisher)queryPublisherResults.Entities[0];
crmSdkPublisher.Id = (Guid)SDKPublisherResults.PublisherId;
crmSdkPublisher.CustomizationPrefix = SDKPublisherResults.CustomizationPrefix;
if (SDKPublisherResults == null)
crmSdkPublisher.Id = CrmService.Create(crmSdkPublisher);
return crmSdkPublisher;
catch (FaultException<OrganizationServiceFault>)
throw;
public Publisher RetrieveDefaultPublisher(string organizationName)
try
string DefaultPublisherPrefix = "DefaultPublisher";
Publisher DefaultPublisher = RetrievePublisherByName(DefaultPublisherPrefix, organizationName);
return DefaultPublisher;
catch (FaultException<OrganizationServiceFault>)
throw;
public Publisher RetrievePublisherByName(string defaultPublisherPrefix, string organizationName)
Publisher DefaultPublisher = new Publisher();
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
QueryExpression queryDefaultPublisher = new QueryExpression
EntityName = Publisher.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression()
;
queryDefaultPublisher.Criteria.AddCondition("uniquename", ConditionOperator.Equal, defaultPublisherPrefix + organizationName);
Entity publisherEntity = CrmService.RetrieveMultiple(queryDefaultPublisher).Entities[0];
if (publisherEntity != null)
DefaultPublisher = publisherEntity.ToEntity<Publisher>();
return DefaultPublisher;
public Solution CreateSolution(string uniqueName, string friendlyName, Guid publisherId, string description, string version)
try
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
Solution solution = new Solution
UniqueName = uniqueName,
FriendlyName = friendlyName,
PublisherId = new EntityReference(Publisher.EntityLogicalName, publisherId),
Description = description,
Version = version
;
QueryExpression querySampleSolution = new QueryExpression
EntityName = Solution.EntityLogicalName,
ColumnSet = new ColumnSet(),
Criteria = new FilterExpression()
;
querySampleSolution.Criteria.AddCondition("uniquename", ConditionOperator.Equal, solution.UniqueName);
EntityCollection querySampleSolutionResults = CrmService.RetrieveMultiple(querySampleSolution);
Solution SampleSolutionResults = null;
if (querySampleSolutionResults.Entities.Count > 0)
SampleSolutionResults = (Solution)querySampleSolutionResults.Entities[0];
solution.Id = (Guid)SampleSolutionResults.SolutionId;
if (SampleSolutionResults == null)
solution.Id = CrmService.Create(solution);
return solution;
catch (FaultException<OrganizationServiceFault>)
throw;
public Solution RetrieveSolution(string uniqueName)
try
Solution solution = new Solution();
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
QueryExpression querySampleSolution = new QueryExpression
EntityName = Solution.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression()
;
querySampleSolution.Criteria.AddCondition("uniquename", ConditionOperator.Equal, uniqueName);
EntityCollection entityCollection = CrmService.RetrieveMultiple(querySampleSolution);
if (entityCollection != null && entityCollection.Entities.Count > 0)
Entity solutionEntity = entityCollection.Entities[0];
if (solutionEntity != null)
solution = solutionEntity.ToEntity<Solution>();
else
querySampleSolution.Criteria = new FilterExpression();
querySampleSolution.Criteria.AddCondition("friendlyname", ConditionOperator.Equal, uniqueName);
entityCollection = CrmService.RetrieveMultiple(querySampleSolution);
if (entityCollection != null && entityCollection.Entities.Count > 0)
Entity solutionEntity = entityCollection.Entities[0];
if (solutionEntity != null)
solution = solutionEntity.ToEntity<Solution>();
return solution;
catch (FaultException<OrganizationServiceFault>)
throw;
public void DeleteSolution(Entity solution)
try
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
if (solution != null)
CrmService.Delete(Solution.EntityLogicalName, solution.Id);
catch (FaultException<OrganizationServiceFault>)
throw;
public void DeleteSolution(string solutionUniqueName)
try
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
CrmService.Delete(Solution.EntityLogicalName, GetSolutionIdByUniqueName(solutionUniqueName));
catch (FaultException<OrganizationServiceFault>)
throw;
public void ImportSolution(string solutionFilePath)
try
byte[] fileBytes = File.ReadAllBytes(solutionFilePath);
ImportSolutionRequest importSolutionRequest = new ImportSolutionRequest()
CustomizationFile = fileBytes
;
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
CrmService.Execute(importSolutionRequest);
catch (FaultException<OrganizationServiceFault>)
throw;
public string ExportSolution(string outputDir, string solutionUniqueName, bool managed)
try
if (!string.IsNullOrEmpty(outputDir) && !outputDir.EndsWith(@"\", false, CultureInfo.CurrentCulture))
outputDir += @"\";
string ManagedStatus;
if (managed)
ManagedStatus = "Managed";
else
ManagedStatus = "UnManaged";
ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest();
exportSolutionRequest.Managed = managed;
exportSolutionRequest.SolutionName = solutionUniqueName;
ExportSolutionResponse exportSolutionResponse;
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
exportSolutionResponse = (ExportSolutionResponse)CrmService.Execute(exportSolutionRequest);
byte[] exportXml = exportSolutionResponse.ExportSolutionFile;
string filename = solutionUniqueName + "_" + ManagedStatus + ".zip";
File.WriteAllBytes(outputDir + filename, exportXml);
return filename;
catch (FaultException<OrganizationServiceFault>)
throw;
public void RollbackSolution(string uniqueName, string solutionFullPath)
try
DeleteSolution(uniqueName);
ImportSolution(solutionFullPath);
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
CrmService.Execute(new PublishAllXmlRequest());
catch (FaultException<OrganizationServiceFault>)
throw;
public Collection<Solution> RetrieveAllSolutions()
try
Collection<Solution> solutions = new Collection<Solution>();
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
OrganizationServiceContext ServerContext = new OrganizationServiceContext(CrmService);
var items = from item in ServerContext.CreateQuery<Solution>()
orderby item.Version ascending
where item.IsVisible == true
select item;
foreach (var item in items)
solutions.Add(item);
return solutions;
catch (FaultException<OrganizationServiceFault>)
throw;
public Guid GetSolutionIdByUniqueName(string uniqueName)
try
Guid solutionQuery;
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
OrganizationServiceContext ServerContext = new OrganizationServiceContext(CrmService);
solutionQuery = (from item in ServerContext.CreateQuery<Solution>()
where item.UniqueName == uniqueName
select item.SolutionId.Value).Single<Guid>();
return solutionQuery;
catch (FaultException<OrganizationServiceFault>)
throw;
public AddSolutionComponentResponse AddComponentToSolution(componenttype solutionComponentType, Guid componentId, string solutionUniqueName)
try
AddSolutionComponentRequest addSolutionComponentRequest = new AddSolutionComponentRequest()
ComponentType = (int)solutionComponentType,
ComponentId = componentId,
SolutionUniqueName = solutionUniqueName
;
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
return (AddSolutionComponentResponse)CrmService.Execute(addSolutionComponentRequest);
catch (FaultException<OrganizationServiceFault>)
throw;
public void PublishCustomizations()
try
using (CrmProxy = new OrganizationServiceProxy(ProxyUri, CrmConnection.HomeRealmUri, CrmConnection.ClientCredentials, CrmConnection.DeviceCredentials))
CrmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
CrmProxy.Timeout = new TimeSpan(0, 10, 0);
CrmService = (IOrganizationService)CrmProxy;
CrmService.Execute(new PublishAllXmlRequest());
catch (ApplicationException)
throw;
Solution 类使用 CrmConnection 类,如下所示:
public class XrmConnection
public XrmConnection()
public XrmConnection(string discoveryServer, string port, string scheme, string organization, string domain, string userName, string password)
DiscoveryServer = discoveryServer;
Port = port;
Scheme = scheme;
Domain = domain;
UserName = userName;
Password = password;
Organization = organization;
InstantiateOrganization();
InstantiateConnection();
public string DiscoveryServer get; set;
public string Port get; set;
public string Scheme get; set;
public string Organization get; set;
public string Domain get; set;
public string UserName get; set;
public string Password get; set;
[CLSCompliant(false)]
public CrmConnection Connection get; set;
private Uri discoveryServiceUri get; set;
private OrganizationDetailCollection Orgs get; set;
private OrganizationDetail Org get; set;
private RetrieveOrganizationsRequest OrgRequest get; set;
private RetrieveOrganizationsResponse OrgResponse get; set;
private OrganizationDetail orgDetail get; set;
private void InstantiateOrganization()
ClientCredentials clientCredentials = new ClientCredentials();
if (!string.IsNullOrEmpty(Domain))
clientCredentials.Windows.ClientCredential.Domain = Domain;
if (!string.IsNullOrEmpty(UserName))
clientCredentials.Windows.ClientCredential.UserName = UserName;
if (!string.IsNullOrEmpty(Password))
clientCredentials.Windows.ClientCredential.Password = Password;
if (string.IsNullOrEmpty(UserName))
clientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
if (!string.IsNullOrEmpty(Port))
discoveryServiceUri = new Uri(String.Format(CultureInfo.CurrentCulture, "0://1:2/XRMServices/2011/Discovery.svc", Scheme, DiscoveryServer, Port));
else
discoveryServiceUri = new Uri(String.Format(CultureInfo.CurrentCulture, "0://1/XRMServices/2011/Discovery.svc", Scheme, DiscoveryServer));
using (DiscoveryServiceProxy ServiceProxy = new DiscoveryServiceProxy(discoveryServiceUri, null, clientCredentials, clientCredentials))
Orgs = DiscoverOrganizations(ServiceProxy);
Org = FindOrganization(Orgs);
Organization = Org.UniqueName;
private OrganizationDetailCollection DiscoverOrganizations(IDiscoveryService service)
try
OrgRequest = new RetrieveOrganizationsRequest();
OrgResponse = (RetrieveOrganizationsResponse)service.Execute(OrgRequest);
return OrgResponse.Details;
catch (FaultException<OrganizationServiceFault>)
throw;
private OrganizationDetail FindOrganization(OrganizationDetailCollection orgDetails)
try
orgDetail = null;
foreach (OrganizationDetail detail in orgDetails)
if (String.Compare(detail.UniqueName, Organization, CultureInfo.CurrentCulture, CompareOptions.None) == 0)
orgDetail = detail;
break;
if (String.Compare(detail.FriendlyName, Organization, CultureInfo.CurrentCulture, CompareOptions.None) == 0)
orgDetail = detail;
break;
return orgDetail;
catch (FaultException<OrganizationServiceFault>)
throw;
private void InstantiateConnection()
Connection = new CrmConnection();
string connectionString = string.Format(CultureInfo.CurrentCulture, "Url=0://1/2", Scheme, DiscoveryServer, Organization);
if (!string.IsNullOrEmpty(Port))
connectionString = string.Format(CultureInfo.CurrentCulture, "Url=0://1:2/3", Scheme, DiscoveryServer, Port, Organization);
if (!string.IsNullOrEmpty(Domain))
connectionString = string.Format(CultureInfo.CurrentCulture, "0; Domain=1", connectionString, Domain);
if (!string.IsNullOrEmpty(UserName))
connectionString = string.Format(CultureInfo.CurrentCulture, "0; Username=1", connectionString, UserName);
if (!string.IsNullOrEmpty(Password))
connectionString = string.Format(CultureInfo.CurrentCulture, "0; Password=1", connectionString, Password);
Connection = CrmConnection.Parse(connectionString);
if (string.IsNullOrEmpty(UserName))
Connection.ClientCredentials = new ClientCredentials();
Connection.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Connection.DeviceCredentials = Connection.ClientCredentials;
我怀疑这可能是 DNS 或其他网络问题。有人可以帮忙吗?
【问题讨论】:
如果您导航到组织服务 URL,会发生什么? 它导航到 URL 没有问题。 【参考方案1】:您是仅使用 Import Solution 方法还是同时使用 CreateSolution Method、CreatePublisher 等...????
几个月前,我用导出/导入请求做了一个类似的例子,它对我来说很好。
http://crmandcoffee.wordpress.com/2013/05/22/import-export-dynamics-crm-2011-solutions-through-web-services/
您为什么不尝试在单独的控制台应用程序中隔离导入请求,从硬盘驱动器获取解决方案并测试 EndPoint?也许您会发现导入工作正常,但您使用解决方案类创建的解决方案不正确。
我建议这样做,因为如果您可以导航到组织 URL,那绝对没问题。
请发表您的答案,我正在开发一个框架来自己处理解决方案,我对您的问题非常感兴趣;)
谢谢,
马里奥
【讨论】:
此代码在许多其他项目上运行良好,但我当前的项目存在特定问题,并且仅使用 ImportSolution 方法。我包括了其他工作正常的方法,以强调它们基本上使用相同的功能来调用 Dynamics CRM 服务的 Execute 方法。例如,ExportSolution 方法工作正常。 您是否尝试过使用 Fiddler 来查看它是否可以为您提供更多信息? 好主意 - 让我看看。 你对 Fiddler 有什么好运吗? 我看到对服务的三个成功调用,然后第四个给出 404 错误。我应该寻找什么具体的东西?以上是关于EndpointNotFound 异常 - Dynamics CRM 2011的主要内容,如果未能解决你的问题,请参考以下文章
Silverlight 故障传播和 UserNamePasswordValidator
如何将 Box<dyn Error + Sync + Send> 转换为 Box<dyn Error>