Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Calling webService (managed C# webservice) from MFC(unmanaged) Application by Retaining the Session using Cookies Container.

0.00/5 (No votes)
11 Sep 2004 1  
This article deals with calling webService (managed C# webservice) from MFC (unmanaged) application by retaining the session using Cookies Container

Introduction

This article deals with calling webservice (managed C# webservice) from MFC (unmanaged) application by retaining the session using Cookies Container.

Description

This article deals with the following factors:

  • Calling managed webservice from MFC Application.
    Solution: Create MFC Dialog Based application, say FirstWebServiceClientDlg.
    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.

  1. Make managed C++ object as member variable inside unmanaged class.

    Solution:

    include<vcclr.h>
                       
    //Add in the dialog Class    
    //Check FirstwebServiceClient.h
    gcroot<webserviceobject*>  m_pWebServiceObject;
  2. To use the cookiescontainer objects like in C#

    Solution:

    using namespace System::Net;
    // And Add in the dialog Class   //Check FirstwebServiceClient.h
    gcroot<CookieContainer*> m_pCookies ; 
    //CookiesContainer is used Enable  Session. In the Sample Code

Sample Code Description

There are two folders:

  • FirstWebServiceClient is an MFC Project
  • FirstWebService is a C# project, used to create WebService

webService contains two methods - one for setting values, another for adding values
Set this folder for WebSharing (Name FirstwebService).

Set values, create two session variables to store the values. Add values to add previously stored values.

Client Code

Before SetValue is called in the client side, you have to enable the cookies.

void CFirstWebServiceClientDlg::OnBnClickedButtonSetval()
{   
  UpdateData(TRUE);
  m_pWebService->CookieContainer = m_pCookies;
  m_pWebService->SetValues(m_oNum1,m_oNum2);
  AfxMessageBox("Now Press Add Button")
} 

Then press Add Button. You will get the correct answer.

Just Comment line
//m_pWebService->CookieContainer = m_pCookies; 

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.

History

  • 12th September, 2004: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here