Metadata contains a reference that cannot be resolved The remote server returned an error: (401) Unauthorized

crmdynamic-proxydynamics-crm-2011microsoft-dynamics

I have windows service which access Microsoft Dynamics CRM 2011.

I have a problem windows service giving exception when it trying to connect to CRM 2011.

It works fine when we run service directly from server.

I have an administrator privileges on CRM Organization what I am trying to connect.

This is the code where connecting to CRM:

  private OrganizationServiceProxy BuildCRMServiceProxy()
     {
         var credentials = new ClientCredentials();            
         var orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(OrganizationUri);
         return new OrganizationServiceProxy(orgConfigInfo, credentials);
     }

The exception is as below:

ERROR 2013-08-21 09:27:47,958 [8] System.InvalidOperationException:
Metadata contains a reference that cannot be resolved:
'http://***/XRMServices/2011/OrganizationData.svc?wsdl'. —>
System.Net.WebException: The remote server returned an error: (401)
Unauthorized. 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.ServiceConfiguration1..ctor(Uri serviceUri,
Boolean checkForSecondary) at
Microsoft.Xrm.Sdk.Client.ServiceConfiguration
1..ctor(Uri serviceUri)
at
Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration.AdjustServiceEndpoint(Uri
serviceUri) at
Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri
serviceUri) at
Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration[TService](Uri
serviceUri) at
*.DynamicsUpdater.ServiceAgent.DynamicsAgent.BuildCRMServiceProxy() at
**.DynamicsUpdater.ServiceAgent.DynamicsAgent.InsertEntity(String entityName, Boolean isRelationship, Dictionary2 attributes, Guid&
generatedId) at
******.DynamicsUpdater.BusinessLogic.CRMProcessorFactory.<>c__DisplayClass3d
1.b__3c(Update
update, ICRMRepository CRM, IUpdatesRepository updatesRepo) at
**.DynamicsUpdater.BusinessLogic.Controllers.ApprovedUpdateController.<>c_DisplayClass3.b_1(Boolean
wasProcessedYet, Func4 processor) at
System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable
1
source, TAccumulate seed, Func3 func) at
******.DynamicsUpdater.BusinessLogic.Controllers.ApprovedUpdateController.Process(Update
update) at
******.DynamicsUpdater.BusinessLogic.Controllers.UpdateControllerBase.Execute(Func
1
retrieveUpdateForProcessingCallback) at
**.DynamicsUpdater.Updater.<>c_DisplayClassb.b_a()


Its work fine when we run it from windows server 2008 but not when I run from my desktop windows 7 64 bit.

Best Answer

Looks like you're not providing valid credentials to the server. Is your desktop in the same network as the server? If so, try NetworkCredential:

credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain");
Related Topic