Quantcast
Channel: CRM Development Forum
Viewing all articles
Browse latest Browse all 888

Occasional MessageSecurityException with Active Directory authentication when using OrganizationServiceProxy

$
0
0

Basically I have a windows service (NServiceBus) on one machine which is inserting and updating CRM using OrganizationServiceProxy. Occasionally, but way too often I get a MessageSecurityException when doing an operation.

System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail. ---> System.ServiceModel.FaultException: The security context token is expired or is not valid. The message was not processed.
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.ProcessReply(Message reply, SecurityProtocolCorrelationState correlationState, TimeSpan timeout)
   at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
   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 Microsoft.Xrm.Sdk.IOrganizationService.Create(Entity entity)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.CreateCore(Entity entity)
   at Gjensidige.TeradataToDynamicsCrm.Dynamics.OrganizationServiceExtensions.CreateWithCatch(IOrganizationService service, Entity entity)
   at Gjensidige.TeradataToDynamicsCrm.Dynamics.DynamicsContext.CreateProspect(Lead prospect, Guid& accountId, Guid& opportunityId)
   at Gjensidige.TeradataToDynamicsCrm.Handler.ProcessCreateCommandHandler.Handle(CreateProspect message)

The timespan a proxy actually live is very short, a few seconds at most, so the timeout is really out of the question...and the same credentials is used all the time and works. I've tried a few different approaches to solve the issue, but it still happens and I can't really figure out why.

The proxy is created like this:

var organizationUri = new Uri(ConfigurationManager.AppSettings["OrganizationUri"]);
var credentials = new ClientCredentials
{
     Windows =
     {
         ClientCredential = new NetworkCredential(ConfigurationManager.AppSettings["Username"],
                                                             ConfigurationManager.AppSettings["Password"],
                                                             ConfigurationManager.AppSettings["Domain"])
     }
};
var proxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
            proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
            proxy.EnableProxyTypes();

I've tried to remedy the problem with this code:

OrganizationResponse response;
try
{
    response = service.Execute(request);
}
catch (MessageSecurityException)
{
    Logger.Warn("MessageSecurityException, retrying");
    using (var proxy = ProxyFactory.Create())
    {
        response = proxy.Execute(request);
    }
}
return response;

This alone doesn't actually work. At times the second execute also fails. What _seems_ to work is to let the service run with the same user as I'm authenticating with, but in my opinion this shouldn't really happen at all, or at least a lot more rare than it currently does. Anyone got any tips?


Viewing all articles
Browse latest Browse all 888

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>