Use .NET dll as COM - c#

I have a somewhat convoluted issue where I need to call a WCF web service from a SQL Server 2005 stored procedure.
While researching I discovered that the only version of .NET SQL Server 2005 will natively support it 2.0 - yet I needed to use a client to consume the web service written for .NET 4. After playing around with some workaround to try to get SQL Server to recognize a .NET 4 DLL I decided to simply create a .NET 2 CLR stored procedure and use a wrapper class written in .NET 4 exposed as COM in order to call my web service.
I found several guides dealing with how to use a .NET 4 dll with .NET 2 as a COM dll (link, link) yet am still struggling.
My "wrapper class" (.NET 4) looks like this:
namespace Wrapper
{
[Guid("4C1D992C-4965-49B2-A694-33810A3B9775")]
[ComVisible(true)]
public class WrapperClass
{
public bool Process(Guid ID)
{
}
}
}
I've added the app.manifest file as suggested by the second link above. However, when I add the generated DLL to my .NET 2 sql server project, it fails to build with the following message:
1>c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1578,5): Warning: MSB3274: The primary reference "Wrapper" could not be resolved because it was built against the ".NETFramework,Version=v4.0" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v2.0".
What am I doing wrong here? I tried not adding the DLL as a reference at all but then the code does not even compile.
Alternatively, is there another (easier) way I can approach this issue?
EDIT: I can't directly add a web reference either:

I ended up just using John Koerner's suggestion and generating a client using WSDL that didn't use System.ServiceModel. As a result I made my CLR assembly .net 2.0 and it worked fine.

Related

SqlClient not supported on this platform

I have a project for Azure Functions that has .net standard 2.0 as target framework. This project uses System.Data.Sqlclient to insert and fetch data from a db. It works fine when I deploy it to azure.
To be able to test my code I have created classes in this project that handle the business logic and is setup in the static function that azure executes.
My problem is that when I try to create a class library that is to contain unit tests, I always get an exception that says: System.Data.SqlClient is not supported on this platform. The code runs fine before that. I have tried both .net core and .net 4.6.1 as target framework but no luck.
It worked when I created a new class library with .net core 2 as target. Don't know why it didn't work the first time so unfortunately I cannot tell what went wrong the first time.

TypeLoadException Could not load type 'WebConfigurationManager' when calling .Net Framework Library via Web API Core app

I have a Web API application created using .Net Core 2.1. A controller from within this web application calls a Business Layer Class library that I also developed using .Net Core 2.1. So far so good...
The .Net Core Business Class Library references a commonly used .Net Framework 4.6.1 Class Library that we have also developed. This library is primarily used to communicate with Azure service bus queues.
As a result this commonly used .Net Framework Class Library in turn references and makes use of the Microsoft .Net Framework Assembly, Microsoft.ServiceBus as shown in the line of code below.
MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(configValue);
As you can see the line of code above that is within our commonly used .Net Framework Class Library passes a string value (i.e. configValue) to a static method that exists within the Microsoft.ServiceBus assembly.
However, whenever the line of code above executes I get the following Error:
An unhandled exception occurred while processing the request.
TypeLoadException: Could not load type
'System.Web.Configuration.WebConfigurationManager' from assembly
'System.Web, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
Out of interest I added in the following line of code which I get the same error:
string configValue = WebConfigurationManager.AppSettings["connectionString"];
Clearly, the issue is that the System.Web .Net Framework assembly isn't loaded.
However, given that the web application is .Net Core then how can I make this web application load the underlying .Net Framework assembly that the commonly used library requires?
Solutions Considered - (Unsuitable)
I had a similar issue before which was easily solved by installing the necessary NuGet package but on this occasion I have not been able to find such a package.
Our team has another Web API application in .Net Framework which calls this commonly used class library without issue however this is not an option and we need to keep the current Web API application based on .Net Core.
I also considered having the commonly used library re-written as a .Net Standard 2.0 Class Library but this given the size of the library this would not be a realistic approach and it won't necessarily address the underlying issue.
Other Possible Solutions
Load a Web.config from within the .Net Core Web API application that contains a configSections section referencing the assembly System.Web. I tried this but it didn't work which I'm sure if I just didn't apply all steps necessary. Is this possible and if so any suggestions?
Is there an alternative way to load the .Net Framework assembly from within the .Net Core Web API?
Sacrifice using the commonly used library and find an alternative (sample) code base that provides similar functionality (i.e. make use of Microsoft.ServiceBus to work with our Azure queues) but that is either .Net Core or .Net Standard based. But similar to the .Net Standard solution above I am unsure how much functionality would need replicated and also unsure of a starting point. Thoughts?
Thanks!
I had the same issue and after investigating i found that my azure function app run time version is 2.0 ~ and according to documentation version 2 is .NET Core 2 and function app version 1.0 ~ is .NET Framework 4.7 . and my visual studio function app project have .NET Framework 4.6.1 . So, I just had to change my function app version to ~ 1.0 .
In a Nutshell
If you have visual studio function app in .NET Core 2.0 then only use function app version 2.0
If you have visual studio function app in .NET Framework 4.7 then only use function app version 1.0

Making a .net component

I made two functions with matlabe and wanted to make a .net component
I created a new project , added a class , and added files (two functions .m)
but when i'm tring to bult the project i got log warning message and at the end this
error occurs
Invalid .NET framework.
Either the specified framework was not found or is not currently supported.
??? Error using ==> mcc
*Error executing mcc, return status = 1 (0x1).*
There were errors during compilation process.
I know nothing about Matlab but so you have the .Net framework installed? You might need a particular version of .Net, this might be configured in your Matlab environment?
Have a look at this blog post for info on how to set the .Net framework version:
http://itscommonsensestupid.blogspot.co.uk/2009/11/matlab-invalid-net-framework-either.html
The .Net Framework doesn't support any Matlab .m files.
You have to rewrite your matlab scripts by using classes out of the .Net Framework in either C# or VB.Net.
This article looks promising.

Call .NET 4.0 WPF Class Library From .NET 2.0 Application

I have an application written in .NET 4.0 that i compile into a DLL file. I want to be able to use this DLL file in a Windows Forms .NET 2.0 application. I don't need the application to really interact much, except just passing a couple string values to the 4.0 DLL. The DLL is pretty much a bunch of Windows which i converted to UserControls and i just need to be able to reference them to display the UserControls from the 2.0 application without having to use a different exe and having to package them separately. What is the best way to do this? Because when i try to add the reference to the DLL to the 2.0 application, it gives me and error saying the DLL is built in a newer version of .NET so i can't do it that way. Is this where a COM object would come in? Any information and links i would really appreciate, thanks.
If the application can really and truly be called from a 2.0 application then the best approach is to compile it as a 2.0 application. Visual Studio 2010 (and 2008) support the notion of multi-targetted solutions. That is you can use it to compile a project for various versions of the CLR.
I would take the approach of compiling my application twice
Once for 4.0
Once for 2.0
This way you can use the DLL directly in your 2.0 application. No messy COM tricks needed
Calling a .ne 4.0 from a .net 2.0 is indeed possible. The problem is not that, but i dont think you can send the usercontrols across!
Im doing this in a couple of soultions where im passing simple classes from 4.0 to my 2.0 assemblies. The trick is to register the 4.0 assembly as a ServicedComponent (COM+), then share an interface (.net 20) betwewen these assemblies. Then in your .net 2.0 assembly instantiate the ServicedComponent and retrieve an instance of your interface from the "middleware" assembly.
As stated, you can only transfer interfaces through this, and marshalling a complex type as a UserControl i think will be very difficult.

Calling web services dynamically from a console application

I am using this example from microsoft to call web services dynamically using reflection:
http://blogs.msdn.com/b/kaevans/archive/2006/04/27/dynamically-invoking-a-web-service.aspx
Which works fine in a web page project, however in a console application the class
ServiceDescriptionImporter
Is unavailable (doesnt come up in intellisense and isnt recognized as part of the assembly it belongs to -- System.Web.Services.Description). Which is wierd b/c it DOES come up in web projects.
Anyone know why this is? I cannot get the console app to work. This is a VS 2010 project.
ServiceDescriptionImporter is in the System.Web.Services assembly. Add a reference to that, and import the namespace with a using System.Web.Services.Description; statement, and it will work.
I also met this problem.
when I went to change the target framework, I found the default target is .Net Framework 4 client profile, also there has a .Net Framework 4 exist.
I changed the target from .Net Framework 4 client profile to .Net Framework 4, and then it's working, the ServiceDescriptionImporter appeared.
hope this helps for others.

Categories

Resources