Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure function project doesn't copy system.private.servicemodel.dll to the output directory #3568

Closed
pragnagopa opened this issue Oct 5, 2018 · 54 comments
Assignees

Comments

@pragnagopa
Copy link
Member

From @Lxiamail on October 2, 2018 0:35

When Azure function references and uses one of WCF core packages (like system.servicemodel.http), system.private.servicemodel.dll, which system.servicemodel.http depends on, is not copied to output directory together with system.servicemodel.http. The azure function fails for can't find system.private.servicemodel.dll. Looks like other WCF core dependencies (like system.servicemodel.primitives.dll) are copied correctly, but system.private.servicemodel.dll. My guess is that Azure function may not handle the reference assembly, façade and implementation assembly pattern correctly.
Some background of WCF Core packages, system.servicemodel.*.dll (like system.servicemodel.http) are reference assemblies (in ref directory of the package) facade assemblies (in lib directory of the package), The façade assemblies don't have implementation, all they do is typeforward to the implementation assembly system.private.servicemodel.dll. App never directly references to system.private.servicemodel.dll. .NET Corefx follows the same façade and implementation pattern. The difference between WCF Core packages and .NET CoreFX packages is .NET CoreFX are in .NET Core SDK as shared framework, but WCF Core doesn't. This is why mscorlib.dll is not copied to app output directory, but still can be loaded.

OS: windows 10 version 1803 (OS build 17134.286)

VS: VS pro 2017 version 15.7.5

Repro steps:

Create a new Azure function http trigger Project,
Add system.servicemodel.http Nuget package to the reference using NugetPackage manager
Add
Use this repo to file issues in documentation or the Functions Visual Studio tooling.
Adding the follow code in Function1.Run() function to make the function to use something implemented in WCF Core packages.
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
binding.Name = "binding1";

Build and debug the azure function in VS, after the function is trigger by Http request, you will get the following error:
[10/1/2018 11:39:36 PM] Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=67985bea-8a52-431a-8d65-4ded513269ce)
[10/1/2018 11:39:36 PM] Exception during runtime resolution of assembly 'System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a': 'System.InvalidCastException: [A]System.AppDomain cannot be cast to [B]System.AppDomain. Type A originates from 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' in the context 'Default' at location 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.9\System.Private.CoreLib.dll'. Type B originates from 'System.Runtime.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' in the context 'Default' at location 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.9\System.Runtime.Extensions.dll'.
[10/1/2018 11:39:36 PM] at Microsoft.Azure.WebJobs.Script.Description.FunctionAssemblyLoader.ResolveAssembly(Object sender, ResolveEventArgs args) in C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\FunctionAssemblyLoader.cs:line 66'
[10/1/2018 11:39:36 PM] Unable to find assembly 'System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Are you missing a private assembly file?
[10/1/2018 11:39:37 PM] Executed 'Function1' (Failed, Id=67985bea-8a52-431a-8d65-4ded513269ce)
[10/1/2018 11:39:37 PM] System.Private.CoreLib: Exception while executing function: Function1. FunctionApp4: Could not load file or assembly 'System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Copied from original issue: Azure/Azure-Functions#974

@pragnagopa
Copy link
Member Author

From @Lxiamail on October 2, 2018 0:40

#dotnet/wcf#2824

@pragnagopa pragnagopa self-assigned this Oct 5, 2018
@pragnagopa
Copy link
Member Author

@Lxiamail - Did you already try the workaround mentioned in the issue you linked above?

@pragnagopa
Copy link
Member Author

From @Lxiamail on October 3, 2018 19:55

@pragnagopa Yes. Zhenlan's work around in #dotnet/wcf#2824 worked for me. However, there are other customers reported the same issue and asked for long term fix. I'm not sure if the work around worked for all customers. My team owns WCF Core packages. Please feel free to contact us if you need more context of the WCF Core packages.

@pragnagopa
Copy link
Member Author

cc: @fabiocav

@j055
Copy link

j055 commented Oct 25, 2018

This workaround dotnet/wcf#2824 does not work for me in a Function v2 app. The workaround allows me to debug locally but the DLL is not published or is removed if the file is uploaded manually when running in Azure.

csproj used:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.23" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.5.3" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.5.3" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.5.3" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.5.3" />
  </ItemGroup>
  <ItemGroup>
    <WCFMetadata Include="Connected Services" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Update="System.Private.ServiceModel.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <Target Name="CopySPSM" BeforeTargets="Build">
    <Copy SourceFiles="$(USERPROFILE)\.nuget\packages\system.private.servicemodel\4.5.3\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)\bin" />
  </Target>
</Project>

I am interested to know if someone can get this working in a Function v2 app and how this is done. Otherwise this fix needs to be expedited.

@rhythmnewt
Copy link

rhythmnewt commented Oct 25, 2018

I had to create a CopyToPublishDirectory ItemGroup and specify direct path to the assembly, just like in the Build target. This way it's picked up correctly when publishing, and I have the publish working from both local and from DevOps pipeline.

Edit: CopyToOutputDirectory ItemGroup doesn't do anything in this case and I don't even have that in my .csproj

  <!--
  this is temporary workaround for issue https://github.com/dotnet/wcf/issues/2824 
  also requires nuget package
  -->
  <Target Name="CopySPSM" BeforeTargets="Build">
    <Copy SourceFiles="$(USERPROFILE)\.nuget\packages\system.private.servicemodel\4.5.3\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)\bin" />
  </Target>
  <ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\system.private.servicemodel\4.5.3\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" CopyToPublishDirectory="Always" />
  </ItemGroup>
  <!--end workaround-->

@j055
Copy link

j055 commented Oct 26, 2018

Thanks @rhythmnewt, that works for me now.

@pragnagopa
Copy link
Member Author

@fabiocav - Is there anything we can do to address this issue?

@Kazpers
Copy link

Kazpers commented Nov 15, 2018

Thank you @rhythmnewt This "solved" it for me too, so we can at least keep dev process going until a proper fix arrives 👍

@pragnagopa pragnagopa removed their assignment Nov 15, 2018
@fabiocav
Copy link
Member

We rely on the build and move all dependencies and runtime specific dependencies to the output. I'll spend some time investigating this to see if this is truly a runtime/build issue on our side, but in the meantime, the documented workaround seems to be the recommended solution.

@fabiocav fabiocav self-assigned this Nov 16, 2018
@fabiocav fabiocav added this to the Triaged milestone Nov 16, 2018
@Lxiamail
Copy link

@fabiocav this "We rely on the build and move all dependencies and runtime specific dependencies to the output." may be the problem. As I stated early, WCF Core packages build time dependencies are all reference assemblies, and runtime dependencies are facades assemblies. The facades assemblies don't have any implementation, they type forward to system.private.servicemodel.dll, which contains actually implementation. However, system.private.servicemodel.dll is not directly referenced by the application (in this case Azure function). If Azure function build only move the direct dependencies to the output, system.private.servicemodel.dll may be missed.

@fabiocav
Copy link
Member

That would indeed be problematic. The functions are expected to deploy any dependency not expected to be brought in by .NET Core Azure Functions Host, and that is one of them.
I'm tracking some enhancements that would help with this, in functions, but this would be a general issue for customers who expect to have what they're building dynamically loaded by a host environment that does not have a direct reference to System.Private.ServiceModel.dll.

@Lxiamail
Copy link

@fabiocav how does Azure function calculate the app dependencies? Does it look at only direct dependencies or it calculate the transact dependencies as well?

@fabiocav
Copy link
Member

It loads transient dependencies in isolation as well. There are some enhancements I'm working on that may actually help with this issue. I'm running some tests now to validate.

@bartdk-be
Copy link

Small bump.
Facing same issue when trying to call SOAP client (using Connected Services)

@iwaldman
Copy link

iwaldman commented Jan 23, 2019

@bartdk-be Same here. Note that I am using Azure Durable Functions.

@brendan-mcmahon
Copy link

I am also running into this using Durable Functions, and having no luck with the posted workarounds.

@tylerhartwig
Copy link

I'm running into this issue as well, and the given workarounds also don't solve my problem.

However, I've found the version of Azure Tools that Visual Studio downloads is able to run our function, while the version I manually downloaded from Github/npm is not able to.

I'm also facing the same issue in Azure, even with the workaround implemented.

@tylerhartwig
Copy link

@iwaldman @brendan-mcmahon
Are you two having issues with not copying the System.Private.ServiceModel.dll?

I was having an issue with failing to resolve a method in the NetTcp assembly, and arrived at this work around as well. I found that if you revert to the Azure Function Extension version to 2.0.12246 then it fixed the issue for me. The underlying problem was that past that version the powershell worker was added, and Azure was resolving to assemblies in that directory, rather than the one from our function.

Not sure that helps, but thought it may be helpful for some.

@fabiocav
Copy link
Member

fabiocav commented Feb 6, 2019

Been meaning to follow up on this as well to share some recent enhancements made to the runtime. There will be additional tooling work to make this the default behavior, but in the meantime, can you add the following post-build event command to your Azure Functions project:

copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json

This will drop the function deps file in the bin output, and the runtime will use that information to load those assets.

@iwaldman
Copy link

iwaldman commented Feb 7, 2019

Thank you all. Will try the two options.

fabiocav added a commit to fabiocav/TestConcordFunctionApp that referenced this issue Feb 27, 2019
@fabiocav
Copy link
Member

Spent a bit of time looking at this today. The new logic handles this as expected if the deps file is correctly generated, but because of your target, runtime dependencies were not present there (this the default .NET behavior). If you update your target to netcoreapp2.1, that should address the problem.

As an example, I've sent a PR to @ghills' repo to show the diff: https://github.com/ghills/TestConcordFunctionApp/pull/1/files

@ghills
Copy link

ghills commented Feb 27, 2019

Spent a bit of time looking at this today. The new logic handles this as expected if the deps file is correctly generated, but because of your target, runtime dependencies were not present there (this the default .NET behavior). If you update your target to netcoreapp2.1, that should address the problem.

As an example, I've sent a PR to @ghills' repo to show the diff: https://github.com/ghills/TestConcordFunctionApp/pull/1/files

@fabiocav interesting - this does fix it for me.

So I had based the target framework on the documentation at https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#microsoftnetsdkfunctions which lists netstandard2.0. If the deps file is not correctly generated for netstandard2.0 would you consider netcoreapp2.1 the correct target framework guidance?

@fabiocav
Copy link
Member

The default project template has been updated to target .NET Core app. I'll submit a documentation update to do the same there.

@fabiocav
Copy link
Member

I'm closing this as resolved with the information provided above. If others experience similar issues, please open a new issue with details and we'll investigate.

@nero120
Copy link

nero120 commented Feb 28, 2019

@fabiocav the issues does not appear to be fixed as my functions project already targets netcoreapp2.1 and System.Private.ServiceModel.dll is not copied to the correct bin location. After build, I can see it in a subfolder within the projects bin folder (specifically, in bin\Debug\netcoreapp2.1\bin\runtimes\win\lib\netstandard2.0 but as it's not present in bin\Debug\netcoreapp2.1\bin I get a runtime error that the assembly cannot be found. Note: @rhythmnewt's work around above temporarily resolves this issue for me.

In my case, my csproj looks like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.4" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="3.0.4" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.3" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.25" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

dotnet --info results:

.NET Core SDK (reflecting any global.json):
 Version:   2.1.503
 Commit:    4c506e0f35

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.503\

Host (useful for support):
  Version: 2.1.7
  Commit:  cca5d72d48

.NET Core SDKs installed:
  1.0.0-preview2-003131 [C:\Program Files\dotnet\sdk]
  1.0.4 [C:\Program Files\dotnet\sdk]
  1.1.0 [C:\Program Files\dotnet\sdk]
  2.1.4 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.300 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.503 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

@ghills
Copy link

ghills commented Feb 28, 2019

@nero120 I think you still need to add this target per @fabiocav response:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
  </Target>

@nero120
Copy link

nero120 commented Feb 28, 2019

Ah I missed that, thanks @ghills!

@mahl
Copy link

mahl commented Apr 3, 2019

I'm still seeing this issue. Is it fixed?

@IGx89
Copy link

IGx89 commented Apr 3, 2019

@ghills (and @fabiocav), thanks a lot for that workaround, definitely cleaner than the previous workaround we were using that required manually including the DLL. However, that new workaround only partially works as-is: it works great for builds, but has no effect on publishes (very common scenario I assume). Here's an updated workaround that also works for publishes (tested on both MSBuild 15.9 and 16.0):

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
  </Target>
  <Target Name="PostPublish" AfterTargets="AfterPublish">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
  </Target>

(if someone has a cleaner method please share)

@nero120
Copy link

nero120 commented Apr 9, 2019

@IGx89 thank you for that, coincidentally I was just looking into why this problem was occurring in Azure and your solution resolved the issue! 🥇

@Kazpers
Copy link

Kazpers commented Apr 9, 2019

I really don't think this can be called solved if you still need to add manual Postbuild workarounds (note haven't had time to test this myself as I'm in a new job now so the project where this was relevant is no longer available to me).

@brendan-mcmahon
Copy link

I was just testing locally after updating to durable 1.8. It seems like it's working after removing the workarounds I'd implemented, but I didn't see this mentioned in the release notes. Was this addressed? Either intentionally or inadvertently? Or is it a weird fluke that this is working now?

@AtOMiCNebula
Copy link

@fabiocav, this arguably shouldn't have been closed. Or at least, I wouldn't consider this closed until such a time when "workarounds" aren't needed anymore. Would you agree?

I had to alter my custom MSBuild <Target>s slightly from what was provided above. The function.deps.json wasn't getting included in my ZIP package; not sure if the prior solution works if you don't use ZIP package deployments, I didn't bother checking. Here's what I went with:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <!-- https://github.com/Azure/azure-functions-host/issues/3568 -->
  <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
</Target>
<Target Name="PostPublish" BeforeTargets="CreateZipFile">
  <!-- https://github.com/Azure/azure-functions-host/issues/3568 -->
  <Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
</Target>

Only meaningful change from above was revising PostPublish to use BeforeTargets="CreateZipFile".

@Cartan
Copy link

Cartan commented Jun 3, 2019

Still won't work for me, even with the workarounds. I simply cannot compile a function project that contains a service reference. I always get:

C:\Users\nils.nuget\packages\microsoft.net.sdk.functions\1.0.27\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : System.IO.FileNotFoundException: Could not load file or assembly 'System.Private.ServiceModel, Version=4.1.2.4, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Das System kann die angegebene Datei nicht finden.
1>C:\Users\nils.nuget\packages\microsoft.net.sdk.functions\1.0.27\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : File name: 'System.Private.ServiceModel, Version=4.1.2.4, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

...
Error generating functions metadata

@SaebAmini
Copy link

SaebAmini commented Jul 24, 2019

The only workaround that did the job for me was copying the DLL from the runtime folder, to the bin folder, after build and publish events, by adding this to my Azure Function app csproj, I noticed the DLL is copied there, but the function host doesn't know how to find it:

<Target Name="FixForDotnetWcfIssueBuild" BeforeTargets="PostBuildEvent">
  <Copy SourceFiles="$(OutputPath)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" />
</Target>
<Target Name="FixForDotnetWcfIssuePublish" AfterTargets="AfterPublish">
  <Copy SourceFiles="$(PublishDir)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(PublishDir)bin" />
</Target>

@ramito1
Copy link

ramito1 commented Aug 1, 2019

Thank you @rhythmnewt! Your workaround fixed it for me as well (using Azure Functions with SOAP client).

@Tiberriver256
Copy link

For mine to work with MSBuild I had to modify the publish copy step as follows:

<Target Name="FixForDotnetWcfIssuePublish" BeforeTargets="_GenerateFunctionsAndCopyContentFiles">
  <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-514445944 -->
  <Copy SourceFiles="$(PublishDir)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(PublishDir)bin" />
</Target>

@nomansadiq11
Copy link

Yes works for me in local and Azure DevOps

@SaebAmini
Copy link

Updating to 4.6.0 solved this issue for me and I could remove the hack.

@tobyreid
Copy link

@fabiocav, this arguably shouldn't have been closed. Or at least, I wouldn't consider this closed until such a time when "workarounds" aren't needed anymore. Would you agree?

I had to alter my custom MSBuild <Target>s slightly from what was provided above. The function.deps.json wasn't getting included in my ZIP package; not sure if the prior solution works if you don't use ZIP package deployments, I didn't bother checking. Here's what I went with:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <!-- https://github.com/Azure/azure-functions-host/issues/3568 -->
  <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
</Target>
<Target Name="PostPublish" BeforeTargets="CreateZipFile">
  <!-- https://github.com/Azure/azure-functions-host/issues/3568 -->
  <Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
</Target>

Only meaningful change from above was revising PostPublish to use BeforeTargets="CreateZipFile".

I had a similar problem using Google.Ads.GoogleAds =2.5.0 with Azure.Net.Sdk.Functions =1.0.29

Resolved using this method for both Windows/Linux:

<Target Name="PostBuildWindows" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Windows_NT'">
    <!--https://github.com/Azure/azure-functions-host/issues/3568-->
    <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
    <Message Text="PostBuild Copied $(OutDir)bin\function.deps.json" />  
  </Target>
  <Target Name="PostPublishWindows" BeforeTargets="AfterPublish" Condition="'$(OS)' == 'Windows_NT'">
    <!--https://github.com/Azure/azure-functions-host/issues/3568-->
    <Message Text="PostPublish Copied $(OutDir)bin\function.deps.json" />  
    <Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
  </Target>
  <Target Name="PostBuildUnix" AfterTargets="PostBuildEvent" Condition="'$(OS)' != 'Windows_NT'">
    <!--https://github.com/Azure/azure-functions-host/issues/3568-->
    <Exec Command="cp -r $(OutDir)$(ProjectName).deps.json $(OutDir)bin/function.deps.json" />
    <Message Text="PostBuild Copied $(OutDir)bin/function.deps.json" />  
  </Target>
  <Target Name="PostPublishUnix" BeforeTargets="AfterPublish" Condition="'$(OS)' != 'Windows_NT'">
    <!--https://github.com/Azure/azure-functions-host/issues/3568-->
    <Exec Command="cp -r $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin/function.deps.json" />
    <Message Text="PostPublish Copied $(OutDir)bin/function.deps.json" />  
  </Target>

This helps when running functions in a linux Docker container.

@LasseAndersen
Copy link

I get the exception "TypeLoadException: Could not load type 'System.UriTemplate' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

I've tried some of the workarounds regarding copying the dll to the publish bin and none of them worked. Of course with my file structure.

<Target Name="FixForDotnetWcfIssueBuild" BeforeTargets="PostBuildEvent"> <Copy SourceFiles="$(OutputPath)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" /> </Target> <Target Name="FixForDotnetWcfIssuePublish" AfterTargets="AfterPublish"> <Copy SourceFiles="$(PublishDir)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(PublishDir)bin" /> </Target>

I used the azure functions project to make my integration and it uses .NET Core 3.0 and azurefunctions v2.

@IGx89
Copy link

IGx89 commented Oct 24, 2019

That's because Azure Functions v2 is incompatible with the .NET Core 3.0 SDK, I recently found out. See Azure/azure-functions-vs-build-sdk#333 -- it puts the runtimes folder in the wrong spot!

Workaround (in addition to the normal workaround here, AtOMiCNebula's is working great for me) is to add a global.json with the following contents (matching the exact patch version of your latest installed .NET Core 2.x SDK) to the function project root -- that'll force the project to be built with the 2.x SDK:

{
  "sdk": {
    "version": "2.2.402"
  }
}

Here's also an alternative workaround (incorporating the primary workaround) if you'd like to still use the .NET Core 3.0 SDK, or need this fix for Functions v3 as well:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
  </Target>
  <Target Name="PostPublish" BeforeTargets="Publish">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
    <!-- https://github.com/Azure/azure-functions-vs-build-sdk/issues/333 -->
    <Exec Command="move $(PublishDir)\runtimes $(PublishDir)\bin" />
  </Target>

@ghost ghost locked as resolved and limited conversation to collaborators Jan 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests