none
Accessing WCF service from MFC program RRS feed

  • Question

  • I have a very old program written in MFC, let's call it [client]. This [client] accesses a DCOM service. Now the service is no longer available in DCOM, but available in WCF and/or .Net Remote.

    So I need to upgrade my [client] program. Since it is pretty big, I really don't want to rewrite it into WPF or windows form. Is there any easy way to do it? I have several thoughts as listed below:

    1. To do little change and re-compile it with CLR in Visual Studio 2010+. Does that mean then I can access .Net dll and WCF service?

    2. To build a dll, which can access the WCF service, and then let [client] to communicate with this dll. But what kind of dll should it be so it can be accessed by MFC program, and accessing WCF service?

    The programmer of the service is willing to cooperate with me but DCOM service is not an option, but if there is minor change he will help me.

    Is there any good idea about it? 

    Thursday, July 3, 2014 5:06 PM

Answers

  • >1. To do little change and recompile it with CLR in Visual Studio 2010+. Does that mean then I can access .Net dll and WCF service?

    That is a big change.

    >2. To build a dll, which can access the WCF service, and then let [client] to communicate with this dll. But what kind of dll should it be so it can be accessed by MFC program, and accessing WCF service?

    The easiest thing is going to build a .NET dll that exposes a COM interop interface similar to the interface you currently use over DCOM. Then you can create this new COM component with minimal code changes.

    I like to keep it simple and just create a .NET façade class with an AutoDual COM ClassInterface.

    http://msdn.microsoft.com/en-us/library/4fcadw4a(v=vs.110).aspx

    Something like:

        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class MyServiceFacade 
        {
            
            void  ServiceOperation1(int someArg, string someOtherArg)
            {
                using (var proxy = new MyServiceProxy())
                {
                    proxy.ServiceOperation1(someArg, someOtherArg);
                }
            }
    
        }

    David


    David http://blogs.msdn.com/b/dbrowne/


    Thursday, July 3, 2014 5:27 PM

All replies

  • >1. To do little change and recompile it with CLR in Visual Studio 2010+. Does that mean then I can access .Net dll and WCF service?

    That is a big change.

    >2. To build a dll, which can access the WCF service, and then let [client] to communicate with this dll. But what kind of dll should it be so it can be accessed by MFC program, and accessing WCF service?

    The easiest thing is going to build a .NET dll that exposes a COM interop interface similar to the interface you currently use over DCOM. Then you can create this new COM component with minimal code changes.

    I like to keep it simple and just create a .NET façade class with an AutoDual COM ClassInterface.

    http://msdn.microsoft.com/en-us/library/4fcadw4a(v=vs.110).aspx

    Something like:

        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class MyServiceFacade 
        {
            
            void  ServiceOperation1(int someArg, string someOtherArg)
            {
                using (var proxy = new MyServiceProxy())
                {
                    proxy.ServiceOperation1(someArg, someOtherArg);
                }
            }
    
        }

    David


    David http://blogs.msdn.com/b/dbrowne/


    Thursday, July 3, 2014 5:27 PM
  • Hi, David, thanks for the advice.

    Anyway we compiled our old program with /clr option and succeeded. Although we have to remove some "not very important" functionality to avoid some compiling error.

    I think if we are going to work on this project, we have to move on to new techniques.

    But after we compiled it with CLR, it seems we still need to use COM to call functions in a .Net dll? I though we can then directly call functions in .Net dll. Is that the case?

    If anyway we need to use COM to communicate with .Net dll, it seems CLR support has no use to us?

    Wednesday, July 9, 2014 3:46 PM
  • Now I built a solution including my old project and a new .Net dll project. I then add a reference of the new .Net dll into my old project. After I compiled old project with /CLR, now I can use "using namespace xxx" in my old project, and I successfully called a static member function in the .Net dll.

    It seems now I can directly use some functions defined in .Net dll without messing with COM. I will go ahead to try to write more functions in .Net dll, including accessing WCF service and call them from old project.

    Thursday, July 10, 2014 3:17 PM
  • Ok, now I can use the functions defined in .Net dll to access WCF service. But to marshal all structured arguments and results are really annoying. Anyway I dodged using DCOM and now can directly access WCF service (both http and net.tcp) in old MFC program (compiled with /CLR).
    Thursday, July 24, 2014 3:06 PM