简体   繁体   中英

In iisnode, make edge.js pull connection string from web.config

I'm running iisnode and using edge.js to access code in a 3rd party DLL. This code expects there to be a web.config file from which to pull a connection string.

File structure:

\noderoot\bin\my3rdParty.dll
\noderoot\cs\myCode.cs
\noderoot\api.js
\noderoot\server.js
\noderoot\web.config

cs\\myCode.cs

using System;
using System.Threading.Tasks;
using My3rdPartyNamespace;

public class Startup {
    public async Task<object> Invoke(dynamic input) {
        // My3rdPartyObject.DoSomeStuff() looks at ConfigurationManager.ConnectionStrings
        return My3rdPartyObject.DoSomeStuff((string)input.someThing);
    }
}

api.js

import {Router} from 'express';
import Edge from 'edge';
import path from 'path';

let router = Router();

router.post('/doAThing', (req, res, next) => {
    let aThing = Edge.func({
        source: path.join(__dirname, 'cs/myCode.cs'),
        references: [
            'System.Web.dll',
            './bin/my3rdParty.dll'
        ]
    });

    let payload = {
        someThing: req.body.someThing
    };

    let response = aThing(payload, true);
    res.json(response);
});

export default router;

I've tried putting the connection string in the web.config file that iisnode uses, but apparently edge.js does not look there. It works when I put the connection string in node.exe.config and place that file in c:\\program files\\nodejs , but this an unacceptable solution.

Again, the whole point of me using edge.js is so I can use a third party DLL, so I cannot simply tell the DLL what my connection string is, unless I can forcibly insert it into ConfigurationManager.ConnectionStrings .

I cannot use edge-sql because I'm not writing any SQL--the dll already takes care of the SQL. I cannot set my connection string using the environment variable EDGE_SQL_CONNECTION_STRING because the dll is looking for a specifically named connection string in web.config .

Ideas?

Edge.js is unaware of being hosted in IIS using iisnode and consequently does not use web.config. iisnode acts as an HTTP reverse proxy to a node.exe that runs an HTTP listener which is part of your application. This node.exe and any code within behaves as a standalone application you could otherwise launch from the command line. Because of that, Edge.js will only consider the node.exe.config file in the same directory as the node.exe executable that runs the application ( https://github.com/tjanczuk/edge#how-to-appconfig ).

I assume placing the connection string in c:\\program files\\nodejs is unacceptable because it is shared across several applications on the machine, possibly run by different users.

There is a way around it. You can configure iisnode to run node.exe in a custom location using the nodeProcessCommandLine configuration property ( https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/iisnode.yml#L13 ). Specifically, you can upload node.exe as part of your application, point nodeProcessCommandLine to use that private copy, and then place node.exe.config with your connection string next to that private node.exe.

As a side note, I recommend placing the call to Edge.func in the code snippet above in the global scope so that it runs just once rather than on every HTTP call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM