DEPARTED
DEPARTED

Reputation: 265

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. Dll is using Web Service.

This is how my App.config looks like:

<?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>

Upvotes: 0

Views: 1220

Answers (2)

Ayo I
Ayo I

Reputation: 8124

You can't hard-code the App.config itself, because it simply isn't designed that way.

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.

But there is no way to embed the dll.

.NET does allow assemblies such as dlls to have their own yourdll.dll.config file. But you can't embed it. It must sit alongside you dll, just as it does for executables.

======= 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.

In WCF you can configure your endpoint in code instead of using an 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". That should help show you how to programmatically create a WCF endpoint.

Upvotes: 2

Tyler
Tyler

Reputation: 484

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"]

Edit: Doing that doesn't rewrite the App.config it simply temporarily adds/changes it

Upvotes: 0

Related Questions