none
AccessViolationException on CertFindCertificateInStore RRS feed

  • Question

  • Hi,

    I'm getting AccessViolationException when calling CertFindCertificateInStore in a wcf service. If the code is used outside the wcf service it works as expected without any errors. Please find my code below and stack trace.

    Dim cert As Microsoft.Web.Services.Security.X509.X509Certificate = Nothing
                Dim store As Microsoft.Web.Services.Security.X509.X509CertificateStore = Nothing
    
                store = Microsoft.Web.Services.Security.X509.X509CertificateStore.LocalMachineStore(Microsoft.Web.Services2.Security.X509.X509CertificateStore.MyStore)
                If Not store.OpenRead() Then Return Nothing
                Dim certs As Microsoft.Web.Services.Security.X509.X509CertificateCollection = store.FindCertificateBySubjectString(SubjectName)
    An unhandled exception of type 'System.AccessViolationException' occurred in Microsoft.Web.Services.dll
    
    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    
    System.AccessViolationException was unhandled
      HResult=-2147467261
      Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
      Source=Microsoft.Web.Services
      StackTrace:
           at Microsoft.Web.Services.Security.X509.NativeMethods.CertFindCertificateInStore(UInt32 hCertStore, CertEncodingType dwCertEncodingType, UInt32 dwFindFlags, UInt32 dwFindType, String name, IntPtr pPrevCertContext)
           at Microsoft.Web.Services.Security.X509.X509CertificateStore.FindCertificateBySubjectString(String subjectsubstring)
           at SignatureDLL.TFSR.RouteOne.CRouteOne.GetX509Certificate(String SubjectName)
           at SyncInvokeSendRouteOneNotesLegacy(Object , Object[] , Object[] )
           at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
           at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
           at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
           at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
           at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
           at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)
           at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
           at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
           at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)
           at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
           at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
           at System.ServiceModel.Channels.TransportDuplexSessionChannel.TryReceiveAsyncResult.OnReceive(IAsyncResult result)
           at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
           at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
           at System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsyncResult.OnReceiveComplete(Object state)
           at System.ServiceModel.Channels.SessionConnectionReader.OnAsyncReadComplete(Object state)
           at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e)
           at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
           at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
           at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

    Please review the code and stack trace. Thanks in advance.

    Tuesday, July 12, 2016 9:08 PM

All replies

  • Hi SandeepPinniti,

    >> I'm getting AccessViolationException when calling CertFindCertificateInStore in a wcf service

    How did you host your service? Did you host it in IIS or Self-host? It would be helpful if you could share us detailed steps to reproduce your issue. Based on this error message, it seems to be related with permission. I made a test with your code in Console application self-host, and it works correctly. I suggest you try below steps to check whether it will work for you.

    1. Create a WCF service and host it in self-host Console application

    2. Switch to debug folder, and launch the service by Right click and Run as administrator

    3. Call the service from client

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 14, 2016 5:11 AM