asp.net-web-api2publishingslowcheetahxdt-transform

Xdt transformations in referenced class library project?


I have a solution with these three projects.

The DAL project is a class library that has a web reference. Thus, the app.config in that project has a section like this:

<applicationSettings>
    <Company.Project.Domain.Properties.Settings>
      <setting name="Company_Project_Domain_Some_Service" serializeAs="String">
        <value>http://my.server.local:8888/somePath/service.asmx</value>
      </setting>
    </Company.Project.Domain.Properties.Settings>
  </applicationSettings>

I have slow cheetah installed and am using config transforms in this DAL project. For example, I have a app.production.config that transforms the above web reference to point to the production web reference like so:

<applicationSettings>
    <Company.Project.Domain.Properties.Settings>
      <setting name="Company_Project_Domain_Some_Service" serializeAs="String">
        <value>http://my.PRODUCTIONSERVER.local:8888/somePath/service.asmx</value>
      </setting>
    </Company.Project.Domain.Properties.Settings>
</applicationSettings>

When I publish the API, the web.config doesn't contain ANY application setting shown above. I can use reflector to drill into the DAL.dll and see the service.asmx path. However, it doesn't do the transform so the published app does NOT use the my.PRODUCTIONSERVER.local:8888.

Thus two questions.

  1. Why does publishing NOT use the xdt transform in the referenced class library?
  2. If the application settings block MUST be in the web.config of the Web API project, does that mean I should remove the web reference from the DAL and add it to the Web API project? ...or can I just leave the reference alone and copy the relevant applicationSettings block to the web.config?

Solution

  • Firstly adding web references into a library does not seem like a good idea. Because libraries are reusable pieces of code that are self contained and can be used across different projects. Based on that logic I'm curious to know why you chose to separate out DAL into a project instead of another folder inside the web API. But I'll leave that discussion for later.

    To answer your first question,

    Why does publishing NOT use the xdt transform in the referenced class library?

    Publishing only transforms configs inside the web app from which you are publishing. I'm not sure how you are publishing you app but if you used command line you'd probably do something like this.

     msbuild /p:PublishOnBuild WebApi.csproj
    

    MsBuild uses your .csproj file to build and publish your app. And within the .csproj contains the information needed for transformation. Hence MsBuild does NOT know that it needs to transform app.config from the referenced library which is why your config from DAL is not transformed but just copied.

    Moving on to your second question

    If the application settings block MUST be in the web.config of the Web API project, does that mean I should remove the web reference from the DAL and add it to the Web API project? ...or can I just leave the reference alone and copy the relevant applicationSettings block to the web.config?

    You have a few options here,

    1. Move the web reference to the Web API project like you've mentioned and control the URI in web.config. This means you need to reference your Web API from DAL.
    2. Move the app settings block to web.config but keep your web reference in your DAL. Then you could set the URI of your service at runtime. Like shown here
    3. [Ideal] Merge DAL into Web Api. I feel this is ideal because unless you want to share DAL across projects there is no reason to move it into a separate project. In fact you seem to be facing this problem because you moved it out in the first place. So unless you have a strong reason to do so this is the best option.