0

i have been trying to access WCF service function(Which loads C++ Dll's too) in a C# client project.

When ever i start client program it fetches the service, loads the dll, but suddenly WCF service will be terminated from the system tray.

I have added timeout parameters in web.config(Service) and app.config(Client).

i have tried to log the error as said here but still its not logging the errors.

what's causing this error, any help will be appreciated.

Update 1 Here's my trace.svclog

Update 2 Here's my entire Code This contains 2 solutions, HelloWorld is a C++ Dll & WcfServices Contains both server(C++ Dll'll be loaded) and Client Projects. These projects have all the changes made till now based on internet solutions.

Regards, Jithendra

Community
  • 1
  • 1
Jithendra
  • 351
  • 3
  • 17

3 Answers3

3

Add the following to your hoster Web.Config. After crash open C:\Trace.svclog and look on red line. Look at XML tab and you will see details about error.

<system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name='traceListener' type='System.Diagnostics.XmlWriterTraceListener' initializeData='C:\Trace.svclog'/>
        </listeners>
      </source>
    </sources>
</system.diagnostics>

Ohhh man, finally i get it to work. What you should do is

[DllImport(@"C:\...\Helloworld.dll", CharSet = CharSet.Ansi, EntryPoint = "Encode01", ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]

[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr Encode01();

and

 public string Genkey()
 {
     IntPtr ptr = Encode01();
     return Marshal.PtrToStringAnsi(ptr);
 }
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75
  • Yeah 'll give it a try and let u know – Jithendra Feb 01 '15 at 05:51
  • @ Giorgi Nakeuri , I have added and trace.svclog has been generated, you can find attached trace.svc in my above question. – Jithendra Feb 02 '15 at 04:22
  • It is a weird hosting. I can not manage to download the file. Upload to different hosting please – Giorgi Nakeuri Feb 02 '15 at 06:34
  • @ Giorgi Nakeuri , Sorry for that, changed link. – Jithendra Feb 02 '15 at 10:27
  • @Jithendra, error says The client and service bindings may be mismatched. Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; charset=utf-8 – Giorgi Nakeuri Feb 02 '15 at 10:37
  • No not actually, both server & client config files have basichttpbinding , attaching my whole project for your reference in above project. – Jithendra Feb 02 '15 at 12:15
  • Sorry i can't upload original Projects as it is so added the Simple one which throws the same error. – Jithendra Feb 02 '15 at 12:24
  • are you sure there is no conflicts? see answer to this question. http://stackoverflow.com/questions/16226582/how-can-i-force-iis-express-to-run-in-32-bit-mode Can you make 2 version of dll in 32 bit and 64 and test on both? – Giorgi Nakeuri Feb 02 '15 at 13:21
  • loading 32-bit c++ dll in 64 bit iisexpress might be a problem but i have tried to load 32-bit c++ dll in 32-bit iisexpress and the same error appears. here's what i did started 32-bit iisexpress first and started the wcf service and started the C# client app but no success. another way around i have Tried to invoke genkey() function in WCFTestClient which is located in "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\WcfTestClient.exe" still the same exception is shown in pop up window. – Jithendra Feb 03 '15 at 04:12
  • i have a doubt its a silly one, after getting Intptr from c++ dll who's gonna handle it in C#, i think it'll result in memory leak. how to overcome with this prob. – Jithendra Feb 03 '15 at 09:40
  • You should free memory in dll. See this https://social.msdn.microsoft.com/Forums/vstudio/en-US/b2162d42-0d7a-4513-b02c-afd6cdd854bd/how-to-release-unmanaged-memory-pointed-by-intptr?forum=clr – Giorgi Nakeuri Feb 03 '15 at 09:46
1

My problem was in c++ Dll not in WCFService which i was loading in WCF Service. As per this Link explanation, i was doing

this

char* pszReturn = new char[256];

instead of this

char* pszReturn = NULL;
pszReturn = (char*)::CoTaskMemAlloc(ulSize);

this was causing error in returning the value, and in result below exception was thrown.

Content Type application/soap+xml; charset=utf-8 was sent to a service expectingtext/xml; charset=utf-8. The client and service bindings may be mismatched.

Hope it helps somebody and Special Thanks for @Giorgi Nakeuri for leading me to this.

Regards,

Jithendra.

Jithendra
  • 351
  • 3
  • 17
0

Both the above answer by me as well by @Giorgi Nakeuri were right, but when i installed the WCF service as a stand alone service and tried to run the client it again started throwing the same exception.

i use Visual studio 2013 and in .Net 4.5 loading normal unmanaged c++ library into C# is throwing an error(I don't know exactly why), Here's what i did to make it stable.

  1. i converted unmanaged c++ library into managed c++ interop library(Which supports CLR functionalities).
  2. in the same way i have accessed them in C#.

these changes working like charm and much stable.

Hope this improved answer helps someone.

Jithendra
  • 351
  • 3
  • 17