當調用上的Dynamics CRM 2011 service
的Execute
方法,傳遞一個ImportSolutionRequest對象作爲參數,下面EndpointNotFound拋出異常:EndpointNotFound異常 - 動態CRM 2011
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;
}
}
}
解決方案類利用一個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沒有問題。 – JFK007