Wednesday, August 29, 2012

Silverlight-DLL communication issue

Silverlight-DLL communication issue

Problems with Silverlight 4 application. In this application every client session creates a separate process, which calls a DLL.

Communication with DLL is built as in the following callstack (for two functions: one working, another - not).

There are 2 functions in DLL (both work fine):

extern "C" BOOL __stdcall DocRunExternPageDividions(const char *docId, int num_form, int PageNum, int *Vcols, int **Vvalues, int *Hcols, int **Hvalues) {     LOG_START_FUNCTION     BOOL res = 1;     __try {         res = DocRunExternPageDividions1(docId, num_form, PageNum, Vcols, Vvalues, Hcols, Hvalues);     }     __except(ExFilter(GetExceptionInformation()))     {         AfxThrowUserException();     }     LOG_STOP_FUNCTION     return res; }   extern "C" BOOL __stdcall DocRunExternPageBreakRects(const char *docId, int num_form, int PageNum) {     LOG_START_FUNCTION         BOOL res = 1;     __try {         res = DocRunExternPageBreakRects1(docId, num_form, PageNum);     }     __except(ExFilter(GetExceptionInformation()))     {         AfxThrowUserException();     }     LOG_STOP_FUNCTION         return res; } 

To call this functions server has two delegates:

private delegate void DocRunExternPageBreakRectsDelegate(string docId, int DocNum, int PageNum);  private delegate void DocRunExternPageDividionsDelegate(     string docId, int DocNum, int PageNum, out int Vcols, out IntPtr VoutArray, out int Hcols,     out IntPtr HoutArray); 

... two delegate instances and corresponding functions:

    private DocRunExternPageBreakRectsDelegate DocRunExternPageBreakRectsD;                 DocRunExternPageBreakRectsD =                 Marshal.GetDelegateForFunctionPointer(ptrDocRunExternPageBreakRects,                                                       typeof (DocRunExternPageBreakRectsDelegate)) as                 DocRunExternPageBreakRectsDelegate;      private DocRunExternPageDividionsDelegate DocRunExternPageDividionsD;                 DocRunExternPageDividionsD =                 Marshal.GetDelegateForFunctionPointer(ptrDocRunExternPageDividionsD,                                                       typeof (DocRunExternPageDividionsDelegate)) as                 DocRunExternPageDividionsDelegate;      public void DocRunExternPageDividions(string docId, int DocNum, int PageNum, out int[] vert, out int[] horz) {         IntPtr VoutArray, HoutArray;         int vcols, hcols;         DocRunExternPageDividionsD(docId, DocNum, PageNum, out vcols, out VoutArray, out hcols, out HoutArray);         marshal(VoutArray, out vert, vcols);         marshal(HoutArray, out horz, hcols);     }      public void DocRunExternPageBreakRects(string docId, int DocNum, int PageNum) {         DocRunExternPageBreakRectsD(docId, DocNum, PageNum);     } 

Each of these functions is called here (server code):

    public bool PageBreakRects(string docId, int DocNum, int PageNum, out int[] vert, out int[] horz) {         bool result;         vert = null;         horz = null;         Program.WriteUserMessage("Called PageBreakRects(" + docId + ", " + DocNum + ", " + PageNum + ")");         try {             DocRunExternPageBreakRects(docId, DocNum, PageNum);             DocRunExternPageDividions(docId, 0, PageNum, out vert, out horz);             result = true;         } catch (Exception ex) {}         return result;     }      public bool GetPageDividions(string docID, int Id, int pageNumber, out int[] vert, out int[] horz) {         bool result = false;         vert = null;         horz = null;         try {             DocRunExternPageDividions(docID, Id, pageNumber, out vert, out horz);             result = true;         } catch (Exception) {}         return result;     } 

Each of them - are called here:

    public DocDividionsResult PageBreakRects(string docID, int DocNum, int pageNumber) {         var result = new DocDividionsResult();         int[] vert;         int[] horz;         result.Data = new List<object> { Program.DllWrapper.PageBreakRects(docID, DocNum, pageNumber, out vert, out horz) };         result.Vert = vert;         result.Horz = horz;         return result;      }      public DocDividionsResult GetPageDividions(string docID, int formId, int pageNumber) {         var result = new DocDividionsResult();         int[] vert;         int[] horz;         result.Data = new List<object>                       {Program.DllWrapper.GetPageDividions(docID, formId, pageNumber, out vert, out horz)};         result.Vert = vert;         result.Horz = horz;         return result;     } 

Then - within lambda-expressions:

    public bool GetPageDividions(string docID, int formId, int pageNumber, out int[] vert, out int[] horz) {         bool result = false;         int []localVert = null;         int []localHorz = null;          if (_wp != null) {             if (Service<IWPCommunication>.Use(TestService =>             {                 TestService.Test(UserId);             },             WPService =>             {                 DocDividionsResult br = WPService.GetPageDividions(docID, formId, pageNumber);                 if (br != null && br.Data != null && br.Data.Length == 1)                 {                     result = (bool)br.Data[0];                     localVert = br.Vert;                     localHorz = br.Horz;                 }             }, Id, FS) == 0)             {         ...                 result = false;             }         }         vert = localVert;         horz = localHorz;         return result;     }      public bool PageBreakRects(string docId, int DocNum, int PageNum) {         bool result = false;         if (_wp != null)         {             if (Service<IWPCommunication>.Use(TestService =>             {                 TestService.Test(UserId);             },             WPService =>             {                 DocDividionsResult br = WPService.PageBreakRects(docId, DocNum, PageNum);                 if (br != null && br.Data != null && br.Data.Length == 1) {                     result = (bool)br.Data[0];                 }             }, Id, FS) == 0)             {         ...                 result = false;             }         }         return result;     } 

The "Use" function (used above):

    public static int Use(UseServiceDelegate<T> codeTest, UseServiceDelegate<T> codeBlock, string SessionId, FileStream fs, bool throwException) {         IClientChannel texy = (IClientChannel)_testFactory.CreateChannel(new EndpointAddress("net.pipe://localhost/X2WPServiceUID" + SessionId));         IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel(new EndpointAddress("net.pipe://localhost/X2WPServiceUID" + SessionId));         int returnCode = 0;         try {             if (codeTest != null) {                 codeTest((T)texy);                 texy.Close();             }             returnCode = 1;             if (codeBlock != null) {                 codeBlock((T)proxy);                 proxy.Close();             }             returnCode = 2;         } catch(Exception e) {             if (returnCode == 1 && throwException)                 throw e;         } finally {             if (returnCode == 0 && codeTest != null)                 texy.Abort();             else if (returnCode == 1 && codeBlock != null)                 proxy.Abort();         }         return returnCode;     } 

Client communication is omitted as exception is raised on the server side. GetPageDividions function works fine, bug PageBreakRects - not: the line DocDividionsResult br = WPService.PageBreakRects(docId, DocNum, PageNum); throws the following exception:

"The message with Action 'http://tempuri.org/IWPCommunication/PageBreakRects'  cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver)  or a binding/security mismatch between the sender and the receiver.   Check that sender and receiver have the same contract and the same binding  (including security requirements, e.g. Message, Transport, None)." 

It is worse mentioning, that if in function PageBreakRects replace: DocDividionsResult br = WPService.PageBreakRects(docId, DocNum, PageNum); with DocDividionsResult br = WPService.GetPageDividions(docID, formId, pageNumber); then no exception is thrown.

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog