使用 NGINX 設定反向代理 Reverse Proxy Windows Communication Foundation (WCF) 並且使用 Node.js 處理 Web 服務描述語言 (WSDL) 文件

最近有朋友說遇到需要處理 WCF 的東西很頭痛就幫忙看了一下,其實我第一次聽到的時候不知道 WCF 後來去查了一下發現好像是微軟發明的一個應用程式開發介面使用方法跟 Web Service 差不多感覺滿特別的

而因為這個服務需要躲在某個固定 IP 下面,這樣我要使用很麻煩而且我沒辦法控制,所以我就偷偷做了一個 Reverse Proxy 讓我可以控制它 debug 不然好麻煩呀!

NGINX 反向代理

其實設定方法跟普通的反向代理差不多,如果遇到問題的朋友可以參考一下我的設定檔,主要需要設定的地方是 $backend 的 IP 與 server_name 其他應該直接貼上就好理論上設定好打開 http://wcf.clarence.tw/service.svc 應該可以看到一個網頁代表這邊設定好了

server {
    server_name wcf.clarence.tw;

    proxy_connect_timeout 60s;
    proxy_send_timeout 120s;
    proxy_read_timeout 120s;

    location / {
        set $backend "http://192.192.192.192:808";
        proxy_pass $backend;
   
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }

    listen 80;
}

使用 Node.js 處理 WSDL

因為平常我開發都在 Linux 系統上面所以我選擇直接使用 Node.js 處理,再來通常對方會提供一個 C# 的使用文件我的參考文件如下,如此我就可以把它轉成 Node.js 來開發拉!

/**
  程式範例:
  將網址加入服務參考(ServiceReference1):http://IP 資料:8041/service.svc 
*/

using System;
using Test.ServiceReference1;
namespace Test {
    public partial class WebForm1: System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            Client client = new Client();
            Info[] Result = client.GetInfo();
            client.Close();
        }
    }
}

這邊主要需要注意的是 client.GetInfo() 這個位置,它會與 Node.js 要使用到的 class 一樣,理論上轉換完成直接執行就會吃到噴出來的資料了!

const soap = require('soap');

const url = 'http://wcf.clarence.tw/service.svc?wsdl';

const args = {}
soap.createClient(url, function(err, client) {
    client.GetInfo(args, function(err, result) {
        console.log(result);
    });
});

參考資料