Models Builder settings not working as expected in staging. Ambiguous reference.
z
I have a project with the following settings:
Copy code
"ModelsBuilder": {
        "ModelsMode": "SourceCodeManual",
        "ModelsDirectory": "~/../MyProject.Models",
        "ModelsNamespace": "MyProject.Models",
        "AcceptUnsafeModelsDirectory": true,
        "FlagOutOfDateModels": true
    }
This works locally: I generate the models, see them added to my class library, build successfully, and the application runs without issues. However, when deploying to staging, I encounter an ambiguous reference error. It seems to reference Umbraco.Cms.Web.Common.PublishedModels instead of MyProject.Models. It’s almost as if the appSettings.Staging configuration is being ignored. I’ve added the same settings to the base appSettings. I suspect the the staging configuration is ignored; as the back office prompts for connection details during the first boot and then adds the connection string to the default appSettings file. Comparing with another site that has the same settings/setup and generates .cs files in the MyProject.Models folder, my newly deployed site only generates a .dll. I can’t find any differences in the build/publish profiles, Program.cs, appSettings, etc. Is there something obvious I’m missing? Why would it work locally but fail on the server?
h
I can't remember exactly, but you need to set something that tells it to use the staging version over the default
a
When deploying, do you copy the files over? Or also delete everything first? There could be 'old' files already present on the server. Also Is your Staging environment variable correctly set ?
z
I am using a publish profile which sets
ASPNETCORE_ENVIRONMENT
I can see this in the result web.config Program.cs snippet:
Copy code
.ConfigureAppConfiguration((hostContext, configBuilder) =>
        {
            configBuilder
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                .Build();
        });
and then my publish profile (manual deployments for now):
Copy code
<Project>
  <PropertyGroup>
    <DeleteExistingFiles>true</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Staging</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Staging</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
    <SelfContained>false</SelfContained>
    <EnvironmentName>Staging</EnvironmentName>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>net6.0</TargetFramework>
    <ProjectGuid>c55c6690-ad61-453d-bd09-1e9a7a60f20f</ProjectGuid>
  </PropertyGroup>
</Project>
a
But does your server have the environment variabele set to Staging ?
else
hostContext.HostingEnvironment.EnvironmentName
will return something else
z
It's a shared server (annoyingly), so we run both Staging and Production sites, but we've other sites where this is not a problem, the correct value is pulled in. I'll take another look. still, the model builder settings aren't taking in the standard appSettings.json either, so that's not the sole issue.
t
You need to include a reference to the Umbraco.CMS.Web.Common nuget package in your models project.
z
@tekoScott - I had that (else it wouldn't work locally) in the end it was very botched appSettings that weren't being applied correctly, all sorted now.
39 Views