简体   繁体   English

如何在dll文件中硬编码App.Config? C#

[英]How To Hard Code App.Config In dll file? C#

I am writing dll file for SAP B1, i need to embed App.Config in dll, no other option. 我正在为SAP B1编写dll文件,我需要将App.Config嵌入dll中,没有其他选择。 Dll is using Web Service. DLL正在使用Web服务。

This is how my App.config looks like: 这是我的App.config的样子:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="WayBillsSoap">
                    <security mode="Transport" />
                </binding>
                <binding name="WayBillsSoap1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://services.rs.ge/WayBillService/WayBillService.asmx"
                binding="basicHttpBinding" bindingConfiguration="WayBillsSoap"
                contract="WayBillWS.WayBillsSoap" name="WayBillsSoap" />
        </client>
    </system.serviceModel>
</configuration>

You can't hard-code the App.config itself, because it simply isn't designed that way. 您无法对App.config本身进行硬编码,因为它不是那样设计的。

You can create your own config and embed it into your application using, for example, a resource file (.resx) or using constants to embed strings in a custom class. 您可以使用资源文件(.resx)或使用常量将字符串嵌入自定义类中来创建自己的配置并将其嵌入到应用程序中。

But there is no way to embed the dll. 但是没有办法嵌入dll。

.NET does allow assemblies such as dlls to have their own yourdll.dll.config file. .NET确实允许诸如dll之类的程序集拥有自己的yourdll.dll.config文件。 But you can't embed it. 但是你不能嵌入它。 It must sit alongside you dll, just as it does for executables. 它必须与dll并排放置,就像对可执行文件一样。

======= Edit after you posted your config ======= =======发布配置后进行编辑=======

Ah. 啊。 So here's the question. 所以这是问题。 If you embed your config, that means by definition it can't change. 如果您嵌入配置,则根据定义,这意味着它不能更改。 So since you're okay with your config not changing -- since you want to embed it -- and it looks like you're using WCF, I would suggest you look at programmatically creating your WCF endpoint. 因此,由于您可以不更改配置,因为您可以嵌入它,并且看起来像您在使用WCF,所以我建议您看一下以编程方式创建WCF终结点的方法。

In WCF you can configure your endpoint in code instead of using an App.Config. 在WCF中,您可以使用代码配置端点,而不使用App.Config。 That way you don't need a config at all. 这样,您根本不需要配置。

Unfortunately, teaching your how to do this is beyond the scope of this question, but take a look at this answer: Programatically adding an endpoint . 不幸的是,教您如何执行此操作超出了此问题的范围,但请看一下以下答案:以编程方式添加一个端点 And try this google search: "wcf endpoint programmatically". 并尝试以下Google搜索:“以编程方式WCF端点”。 That should help show you how to programmatically create a WCF endpoint. 这应该有助于您了解如何以编程方式创建WCF端点。

I'm unsure about completely replacing the App.config but you can add and modify things found in it from code using the System.Configuration I've specifically used the System.Configuration.ConfigurationManager.AppSettings["SomeKey"] 我不确定是否完全替换App.config,但是您可以使用System.Configuration从代码中添加和修改在其中找到的内容,我专门使用了System.Configuration.ConfigurationManager.AppSettings["SomeKey"]

Edit: Doing that doesn't rewrite the App.config it simply temporarily adds/changes it 编辑:这样做不会重写App.config,它只是临时添加/更改它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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