This article deals with calling webservice (managed C# webservice) from MFC (unmanaged) application by retaining the session using Cookies Container.
This article deals with the following factors:
- Calling managed webservice from MFC Application.
Solution: Create MFC Dialog Based application, say
Before adding WebService, you have to change the settings of project.
Right click on the Solution Explorer, click properties.
- General - Use Managed Extensions = YES
- C/C++ - General - Debug Information Format = Program Database (/Zi)
- C/C++ - General - Compile As Managed = Assembly Support (/clr)
- C/C++ - Code Generation - Enable Minimal Rebuild = No
- C/C++ - Code Generation - Basic Runtime Checks = Default
Now you are ready to add WebService. Right click on Solution Explorer ->
AddWebService -> in the dialog box, give http://localhost/firstwebservice/firstwebservice.asmx then click Addreference button.
[Note: You have to make Firstwebservice folder websharing that is contained in the sample code.]
VC.NET automatically adds a header file (webservice.h), .cs file and a DLL. In order to call your webservice, you just include webservice.h.
- Make managed C++ object as member variable inside unmanaged class.
- To use the cookiescontainer objects like in C#
using namespace System::Net;
gcroot<CookieContainer*> m_pCookies ;
Sample Code Description
There are two folders:
FirstWebServiceClient is an MFC Project
FirstWebService is a C# project, used to create
webService contains two methods - one for setting values, another for adding values
Set this folder for
Set values, create two session variables to store the values. Add values to add previously stored values.
SetValue is called in the client side, you have to enable the cookies.
m_pWebService->CookieContainer = m_pCookies;
AfxMessageBox("Now Press Add Button")
Then press Add Button. You will get the correct answer.
Just Comment line
Then press Add Button, and compare the result.
The difference is because, when every time you send a Web request, it is treated as a separate request. In order to retain the previous values, use enable session.
- 12th September, 2004: Initial post