wcf icon indicating copy to clipboard operation
wcf copied to clipboard

System.Security.Cryptography.Cng error when trying to add WCF service to .NET6 project

Open Montago opened this issue 3 years ago • 3 comments

I'm using Visual Studio 2022 17.3.1

And I'm getting a series of errors regarding System.Security.Cryptography.Cng when trying to add a WCF service to my WPF .NET6 project.

I'm unsure if this is a WCF error or Visual Studio error - I'm guessing the errors come from the dotnet-svcutil so therefore i report it here.

Importing web service metadata ...
Scaffolding service reference code ...
ErrorMSBuild version 17.3.0+92e077650 for .NET

C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj : warning NU1605: Detected package downgrade: System.Security.Cryptography.Cng from 5.0.0 to 4.5.0. Reference the package directly from the project to select a different version. 

C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj : warning NU1605:  SvcutilBootstrapper -> dotnet-svcutil-lib 2.0.3 -> NuGet.ProjectModel 5.10.0 -> NuGet.DependencyResolver.Core 5.10.0 -> NuGet.Protocol 5.10.0 -> NuGet.Packaging 5.10.0 -> System.Security.Cryptography.Cng (>= 5.0.0) 

C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj : warning NU1605:  SvcutilBootstrapper -> System.Security.Cryptography.Cng (>= 4.5.0)

C:\Program Files\dotnet\sdk\6.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets(250,5): error NETSDK1136: The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so. 

[C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj]

Build FAILED.

C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj : warning NU1605: Detected package downgrade: System.Security.Cryptography.Cng from 5.0.0 to 4.5.0. Reference the package directly from the project to select a different version. 

C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj : warning NU1605:  SvcutilBootstrapper -> dotnet-svcutil-lib 2.0.3 -> NuGet.ProjectModel 5.10.0 -> NuGet.DependencyResolver.Core 5.10.0 -> NuGet.Protocol 5.10.0 -> NuGet.Packaging 5.10.0 -> System.Security.Cryptography.Cng (>= 5.0.0) 

C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj : warning NU1605:  SvcutilBootstrapper -> System.Security.Cryptography.Cng (>= 4.5.0)

C:\Program Files\dotnet\sdk\6.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets(250,5): error NETSDK1136: The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so. 

[C:\Users\mk\AppData\Local\Temp\WCFConnectedService\2022_Aug_22_08_15_46\svcutil_bootstrap\SvcutilBootstrapper\SvcutilBootstrapper.csproj]
    1 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.45
An error occurred while bootstrapping svcutil. This usually happens when processing references.  You might be able to work around this problem by not providing reference parameters, and manually removing any types redefined in the generated proxy code.
Done.
ErrorFailed adding service reference(s). Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Montago avatar Aug 22 '22 06:08 Montago

@Montago I couldn't reproduce the error on the latest version of VS2022 (currently 17.3.2), can you try to upgrade VS and retry? If issue persist, can you provide a standalone repro project?

imcarolwang avatar Aug 25 '22 06:08 imcarolwang

@imcarolwang My problem might be that the project is upgraded from .NET Framework 472 to .NET6

I just wanted to report the error in case it was something obvious i should do.

Montago avatar Aug 25 '22 07:08 Montago

@Montago thank you for reporting the error! Could you provide the repro steps or a standalone repro project? That will be of great help in investigating the root cause of the problem.

imcarolwang avatar Aug 30 '22 03:08 imcarolwang

I was able to reproduce this issue and figured I'd share my findings for those unfortunate enough to experience this problem. In the same solution, I created a brand new WPF .NET 7 project that worked as expected.

Like Montago, I upgraded a WPF/WCF .NET Framework 4.8 project to .NET 7. During this process, the upgrade tool (VS 2022 Extension v0.1.280.25227) modified the .csproj file but didn't remove the WCF references. During troubleshooting, I removed the below lines since they weren't included the new project.

  <ItemGroup>
    <WCFMetadata Include="Service References\" />
  </ItemGroup>
  <ItemGroup>
    <WCFMetadataStorage Include="Service References\ServiceReference\" />
    <!--
    .. snip ..
    -->
  </ItemGroup>

Unfortunately, this alone wasn't enough. I began aggressively commenting out lines in the project file until I only had NuGet package references left. After commenting them all out, I was finally able to add a new service reference without any issues. I began readding a package one at a time until I narrowed it down to a single package causing the problem in my project. In my case, it was the package Prism.Wpf. I do not believe this package is a problem by itself, but I believe the fact that the dependencies list net5.0-windows7.0 is the issue.

Something worth noting is that I was occasionally able to add a single WCF service once in a while without making any modifications. I'm not sure why this worked sometimes but not others, but this package definitely appeared to be the primary issue.

MH-ZShearer avatar May 24 '23 03:05 MH-ZShearer

I was able to reproduce this issue and figured I'd share my findings for those unfortunate enough to experience this problem. In the same solution, I created a brand new WPF .NET 7 project that worked as expected.

Hi @MH-ZShearer Thanks for spending time finding the issue.

I must admit that i've more or less given up on upgrading my WPF application to .NET 6/7 and still rely on the WCF service. The tooling (dotnet-svcutil) for .NET core /6/7 is missing som capabillities that .NET Framework had.

eg:

  • It ignores the settings of creating all classes as INotifyPropertyChanged
  • It ignores the settings of creating Lists as ObservableCollections and instead only use Arrays

So i'm kind of stranded...

Montago avatar May 24 '23 07:05 Montago

I must admit that i've more or less given up on upgrading my WPF application to .NET 6/7 and still rely on the WCF service. The tooling (dotnet-svcutil) for .NET core /6/7 is missing som capabillities that .NET Framework had.

It definitely feels like a struggle with WCF in the project. I'm not sure if WCF is just a dated solution, but it doesn't feel like the tools are retaining the same functionality.

It ignores the settings of creating Lists as ObservableCollections and instead only use Arrays

This can be fixed by using the --collectionType <type> (or -ct) option like so:

dotnet-svcutil project.csproj -n "ProjectNameSpace.ServiceReference" -ct System.Collections.Generic.List`1

It ignores the settings of creating all classes as INotifyPropertyChanged

I believe this can be solved by using the --enableDataBinding (or -edb) option.

You should be able to look at the complete list of help options with the following command:

dotnet-svcutil --help

I'm still trying to figure out how to use the types from another project in the same solution. So far, I've only been able to reference packages.

MH-ZShearer avatar May 24 '23 13:05 MH-ZShearer

It's been a while since i tested the WCF tooling.. and while i was browsing the WCF repo i noticed that they had posted some improvements.

So i just booted up a console .NET 7 app in VS2022 17.6 and tried adding one of my WCF services.

This time i was able to use the ObservableCollection type ! but the INPC was missing.

after finding this issue : https://github.com/dotnet/wcf/issues/5032 i tried editing the JSON file and run the dotnet util.

This time it generated the classes with INPC (databinding) abilitity !

I guess i'm ready to try upgrading some of my applications to .NET 7 now :) although - the tools are a bit crippled - it SHOULD be possible now.

Montago avatar May 24 '23 13:05 Montago

For posterity, I have included the command I was using to create the service references in my project. I used a PowerShell variable, but it isn't required. The provided options most closely matched what we used previously. I was able to get the INotifyPropertyChanged by using the --enableDataBinding option, but it looks like individual experiences may vary.

$svc = "Generic"
dotnet-svcutil "http://server:8084/${svc}Service.svc" `
  --outputDir "Service References/${svc}ServiceReference" `
  --namespace "*, Application.${svc}ServiceReference" `
  --collectionType 'System.Collections.Generic.List`1' `
  --collectionType 'System.Collections.Generic.Dictionary`2' `
  --reference '../DataObject/DataObject.csproj' `
  --sync `
  --enableDataBinding

~~To update the above generated service reference, the following command works:~~ I spoke too quickly, the following does not work with referenced projects and I have not figured out a way to update with the references originally used.

$svc = "Generic"
dotnet-svcutil -u "Service References/${svc}ServiceReference/dotnet-svcutil.params.json"

I would like to see the UI tool in Visual Studio to support more of these options, specifically the project references. And now a way to update the service references using the project references without having to delete and then re-add the references.

MH-ZShearer avatar May 24 '23 18:05 MH-ZShearer

Closing as this issue appears to have been resolved. If you still have problems using the tool (it is recommended to try the latest version), feel free to reactivate or open a new issue.

imcarolwang avatar Jan 17 '24 02:01 imcarolwang