c++ - 为非托管 C++ 客户端创建 WCF 服务

标签 c++ wcf web-services soap wsdl

我需要让非托管的 Windows C++ 客户端与 WCF 服务对话。 C++ 客户端可以在 Win2000 和更高版本上运行。我可以控制 WCF 服务和正在使用的 C++ API。由于它用于专有应用程序,因此最好尽可能使用 Microsoft 的东西,绝对不是 GNU 许可的 API。那些让它工作的人,你能分享一个如何让它工作的分步过程吗?

到目前为止,我已经研究了以下选项:

  • WWSAPI - 不好,不适用于 Win 2000 客户端。
  • ATL 服务器,使用 following guide作为引用。我遵循了概述的步骤(删除策略引用并扁平化 WSDL),但是生成的 WSDL 仍然不能被 sproxy 使用

  • 还有更多想法吗?请仅在您自己实际使用它时才回答。

    编辑 1 :我为任何我可能感到困惑的人道歉:我正在寻找一种从没有安装 .NET 框架的客户端调用 WCF 服务的方法,因此使用基于 .NET 的帮助程序库不是一种选择,它必须是纯非托管 C++

    最佳答案

    基本思想是用 C# 为您的客户端编写 WCF 代码(这样更容易)并使用 C++ 桥 dll 来弥合非托管 C++ 代码和用 C# 编写的托管 WCF 代码之间的差距。

    以下是使用 Visual Studio 2008 和 .NET 3.5 SP1 的分步过程。

  • 首先要做的是创建 WCF 服务和托管它的方法。如果您已经有了这个,请跳到下面的第 7 步。否则,按照 here 中的步骤创建 Windows NT 服务。 .使用 VS2008 为项目和添加到项目的任何类提供的默认名称。此 Windows NT 服务将承载 WCF 服务。
  • 将名为 HelloService 的 WCF 服务添加到项目中。为此,右键单击解决方案资源管理器窗口中的项目并选择添加|新项目...菜单项。在“添加新项”对话框中,选择 C# WCF 服务模板并单击“添加”按钮。这会将 HelloService 以接口(interface)文件(IHelloService.cs)、类文件(HelloService.cs)和默认服务配置文件(app.config)的形式添加到项目中。
  • 像这样定义 HelloService:

  • ``
        [ServiceContract]
        public interface IHelloService
        {
            [OperationContract]
            string SayHello(string name);
        }
        public class HelloService : IHelloService
        {
            public string SayHello(string name)
            {
                return String.Format("Hello, {0}!", name);
            }
        }
    
  • 将上面第 1 步中创建的 Service1 类修改为如下所示:
    using System.ServiceModel;
    using System.ServiceProcess;
    public partial class Service1 : ServiceBase
    {
        private ServiceHost _host;
        public Service1()
        {
            InitializeComponent();
        }
        protected override void OnStart( string [] args )
        {
            _host = new ServiceHost( typeof( HelloService ) );
            _host.Open();
        }
        protected override void OnStop()
        {
            try {
                if ( _host.State != CommunicationState.Closed ) {
                    _host.Close();
                }
            } catch {
            }
        }
    }
    
  • 构建项目。
  • 打开 Visual Studio 2008 命令提示符。导航到项目的输出目录。键入以下内容: `installutil WindowsService1.exe' 这将在您的本地机器上安装 Windows NT 服务。打开服务控制面板并启动 Service1 服务。这样做很重要,以便下面的第 9 步工作。
  • 打开另一个 Visual Studio 2008 实例并创建一个 MFC 应用程序,它与您从 WCF 获得的距离差不多。例如,我只是创建了一个对话框 MFC 应用程序并添加了一个 Say Hello!按钮。在解决方案资源管理器中右键单击项目并选择属性菜单选项。在常规设置下,将输出目录更改为 ..\bin\Debug。在 C/C++ 常规设置下,将 ..\HelloServiceClientBridge 添加到附加包含目录。在链接器常规设置下,将 ..\Debug 添加到其他库目录。单击确定按钮。
  • 从文件菜单中,选择添加|新建项目...菜单项。选择 C# 类库模板。将名称更改为 HelloServiceClient 并单击 OK 按钮。在解决方案资源管理器中右键单击项目并选择属性菜单选项。在“生成”选项卡中,将输出路径更改为 ..\bin\Debug,以便程序集和 app.config 文件与 MFC 应用程序位于同一目录中。该库将包含对承载在 Windows NT 服务中的 WCF Hello 服务的服务引用,即 WCF 代理类。
  • 在解决方案资源管理器中,右键单击 HelloServiceClient 项目的 References 文件夹并选择 Add Service Reference... 菜单选项。在地址字段中,输入 Hello 服务的地址。这应该等于上面第 2 步中创建的 app.config 文件中的基地址。单击“前往”按钮。 Hello 服务应该显示在服务列表中。点击确定按钮自动生成
    Hello 服务的代理类。 注意:我似乎总是遇到此过程生成的 Reference.cs 文件的编译问题。我不知道是我做错了还是有错误,但解决这个问题的最简单方法是直接修改 Reference.cs 文件。该问题通常是命名空间问题,可以轻松解决。请注意,这是一种可能性。对于本示例,我已将 HelloServiceClient.ServiceReference1 更改为简单的 HelloService(以及任何其他必需的更改)。
  • 为了让 MFC 应用程序与 WCF 服务交互,我们需要构建一个托管的 C++“桥”DLL。从文件菜单中,选择添加|新建项目...菜单项。选择 C++ Win32 项目模板。将名称更改为 HelloServiceClientBridge 并单击 OK 按钮。对于应用程序设置,将应用程序类型更改为 DLL 并选中空项目复选框。单击完成按钮。
  • 首先要做的是修改项目属性。在解决方案资源管理器中右键单击项目并选择属性菜单选项。在常规设置下,将输出目录更改为 ..\bin\Debug 并将公共(public)语言运行时支持选项更改为公共(public)语言运行时支持 (/clr)。在框架下
    和 References 设置,添加对 .NET System、System.ServiceModel 和 mscorlib 程序集的引用。单击确定按钮。
  • 将以下文件添加到 HelloServiceClientBridge 项目 - HelloServiceClientBridge.h、IHelloServiceClientBridge.h 和 HelloServiceClientBridge.cpp。
  • 将 IHelloServiceClientBridge.h 修改为如下所示:
    #ifndef __IHelloServiceClientBridge_h__
    #define __IHelloServiceClientBridge_h__
    
    #include <string>
    
    #ifdef HELLOSERVICECLIENTBRIDGE_EXPORTS
    #define DLLAPI __declspec(dllexport)
    #else
    #define DLLAPI __declspec(dllimport)
    #pragma comment (lib, "HelloServiceClientBridge.lib") // if importing, link also
    #endif
    
    class DLLAPI IHelloServiceClientBridge
    {
    public:
        static std::string SayHello(char const *name);
    };
    
    #endif // __IHelloServiceClientBridge_h__
    
  • 将 HelloServiceClientBridge.h 修改为如下所示:
    #ifndef __HelloServiceClientBridge_h__
    #define __HelloServiceClientBridge_h__
    
    #include <vcclr.h>
    #include "IHelloServiceClientBridge.h"
    
    #ifdef _DEBUG
    #using<..\HelloServiceClient\bin\Debug\HelloServiceClient.dll>
    #else
    #using<..\HelloServiceClient\bin\Release\HelloServiceClient.dll>
    #endif
    
    class DLLAPI HelloServiceClientBridge : IHelloServiceClientBridge
    { };
    
    #endif // __HelloServiceClientBridge_h__
    
  • .cpp 文件的语法使用托管 C++,这需要一些时间来适应。将 HelloServiceClientBridge.cpp 修改为如下所示:
    #include "HelloServiceClientBridge.h"
    
    using namespace System;
    using namespace System::Runtime::InteropServices;
    using namespace System::ServiceModel;
    using namespace System::ServiceModel::Channels;
    
    std::string IHelloServiceClientBridge::SayHello(char const *name)
    {
        std::string rv;
        gcroot<Binding^> binding = gcnew WSHttpBinding();
        gcroot<EndpointAddress^> address = gcnew EndpointAddress(gcnew String("http://localhost:8731/Design_Time_Addresses/WindowsService1/HelloService/"));
        gcroot<HelloService::HelloServiceClient^> client = gcnew HelloService::HelloServiceClient(binding, address);
        try {
            // call to WCF Hello Service
            String^ message = client->SayHello(gcnew String(name));
            client->Close();
            // marshal from managed string back to unmanaged string
            IntPtr ptr = Marshal::StringToHGlobalAnsi(message);
            rv = std::string(reinterpret_cast<char *>(static_cast<void *>(ptr)));
            Marshal::FreeHGlobal(ptr);
        } catch (Exception ^) {
            client->Abort();
        }
        return rv;
    }
    
  • 剩下要做的唯一一件事就是更新 MFC 应用程序以调用 SayHello() WCF
    服务电话。在 MFC 窗体上,双击 Say Hello!按钮以生成 ButtonClicked 事件处理程序。使事件处理程序看起来像这样:
    #include "IHelloServiceClientBridge.h"
    #include <string>
    void CMFCApplicationDlg::OnBnClickedButton1()
    {
        try {
            std::string message = IHelloServiceClientBridge::SayHello("Your Name Here");
            AfxMessageBox(CString(message.c_str()));
        } catch (...) {
        }
    }
    
  • 运行应用程序并单击 Say Hello!按钮。这将导致应用程序
    调用承载在 Windows NT 服务中的 WCF Hello 服务的 SayHello() 方法(顺便说一下,它应该仍在运行)。然后返回值显示在消息框中。

  • 希望您可以从这个简单的示例中推断出满足您的需求。如果这不起作用,请告诉我,以便我修复帖子。

    关于c++ - 为非托管 C++ 客户端创建 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/686452/

    相关文章:

    c++ - 将 + 和 - 之类的运算符放入变量中? C++

    java - WAR 文件外部的库依赖项

    web-services - WSDL、DISCO 和 EVENT 之间有什么区别?

    java - 向客户端广播消息

    c++ - Doxygen——如何链接到 C++ 文件范围全局变量

    c++ - 在系统范围内更改所有窗口文本框的渲染机制

    c++ - 如何使用 Dlib 的多目标检测器?

    c# - ConfigurationManager.ConnectionStrings[0].Name 返回 LocalSqlServer

    c# - 向 WCF RequestSecurityToken 消息添加 header

    .net - IIS7.5 上托管的 WCF netTcpBinding 停止工作