adding COM dll as a reference from specific location - c#

I am posting this query again.Soory, but I dont know how to ask doubts about already asked query.
I am using a COM dll as a reference in my Project. I want that dll to be referenced from any location of computer.
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="MyAssembly" culture="" publicKeyToken="8968ee41e78ce97a" /> <codeBase version="1.0.0.0" href="file://c:/some_path/myassembly.dll" /> </dependentAssembly> </assemblyBinding></runtime>
I have added above mentioned code in App.config file. After signing the Interop.Microsoft.Office.Interop.Excel.dll which was unsigned earlier,I have given proper value for PublicKeytoken.
This I think works fine.
but when i run the application the exe expects the dll to present in same folder & that too
the unsigned version.
Can anyone suggest me, if there is anything which i am missing in my code?
Thanks,
Amit
PS: During coding, I had added reference of unsigned version of dll. from C:\Program files...\ [Already existing dll, microsoft provided]

This is only a guess without reproducing your exact situation... It appears your missing the
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
In all the examples I've seen as well as all my uses of this, the above <assemblyIdentity...> tag is always followed by a <bindingRedirect ...>. BTW, You can also specify the original version as a range like the following:
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="2.0.0.0"/>

I think i have got the solution. As i mentioned in my question, I had added reference of older[unsigned] version of dll. I removed that & added reference of signed dll,then made changes into App.config[mentioned in my question].

Related

different third party dll's have minimum requirement of different versions of same dll [duplicate]

I am trying to use SocketIO4Net to create socket.io client in .net. Itseems SocketIO4Net has a dependency of Newtonsoft.Json >= 4.0.8. I also am using PushSharp library which has a Newtonsoft.Json dependency of >= 4.5.10. I got NewtonSoft.Json 4.5.11 when i first installed PushSharp and I thought this version should support SocketIO4Net as well since its a higher version but i get this error whenever am trying to connect to socket.io server.
Could not load file or assembly 'Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I have been banging my head all day with these dependency issues, I would be very grateful if someone can point me in the right direction.
Found solution, try with:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
You can modify assembly-binding configuration and add a redirect. See Redirecting Assembly Versions on MSDN.
Basically you want to add following snippet to your app.config or web.config file:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json"
publicKeyToken="30ad4fe6b2a6aeed"
culture="neutral" />
<!--
Assembly versions can be redirected in application,
publisher policy, or machine configuration files.
-->
<bindingRedirect oldVersion="1.0.0.0-4.5.11.0" newVersion="4.5.11.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
EDIT
Why do you need to redirect assembly versions? Even though SocketIO4Net supports newer versions of Newtonsoft.Json, it was compiled against a single version (4.0.8 in your case). This version is stored in the DLL and it is used to load DLLs SocketIO4Net depends on.
Note that NuGet dependencies are not the same as DLL/runtime dependencies - NuGet dependency on Newtonsoft.Json >= 4.0.8 only means that you will be allowed to install SocektIO4Net into a project that has a newer version of Newtonsoft.Json, it has nothing to do with runtime settings.
That being said, recent NuGet versions should add assembly-binding-redirects automatically for you if your project has app.config or web.config file.
The above solutions are correct but there is one more point that should not be forgotten: the app.config content was the same as the above solutions.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
But it's a good idea to check if it's up to date. In my case, Newtonsoft.JSON (v.6.0.4) has come to depend on another package.
There are two option;
Update (Newtonsoft.JSON package) last versions.
Update app.config file in the version numbers.
And last advice, if you are working with more than one project, eg.
exe-dll and check both versions if there is Newtonsoft.JSON.
Put in an assembly redirect in your app/web.config;
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="1.0.0.0-4.5.11.0" newVersion="4.5.11.0" />
</dependentAssembly>
Please note the versions numbers need to match the version you have installed.
Had this same issue.
Just resolved it.
It happened after NuGet was used to install Ext.NET which has a dependency for Newtonsoft.JSON.
There was already a Newtonsoft.JSON.dll file in /bin (and obviously a reference to it in the web.config file) folder without checking I started the NuGet Package-Install procedure while debugging(so the file probably had a lock).
On the runtime error window it will tell you on the stack trace what part of the manifest it has a problem with, mine was major version so I checked the install package version. and it was 1 major version out. Found the original NuGet file under: "[physical path]/../packages/Newtonsoft.Json.[version]/lib/[.net version]/"
Both Manifest and Library was there so copied it into /bin folder, updated the root web.config assembly information and it worked.
Code samples:
Before
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
After
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
Hope this helps
In my case, I removed the package with NuGet and installed a fresh one. Then, remove the reference from References and add again manually. Works like charm. Hope resolve for you.
I was working on an old project recently. I needed to update our Newtonsoft.Json.dll, since I had to utilize a "new" API which required a newer version, but I still had other DLLs that required the old version.
bindingRedirect you say? Nope. It kept complaining about the manifest mismatch.
Separate codeBase tags? Nope. It kept complaining about the manifest mismatch.
The problem was, apparently, that the old version of Newtonsoft.Json.dll (3.0.0.0) does NOT have a PublicKeyToken, but the "new" version (4.5.7.1) DOES have a PublicKeyToken. Therefore they couldn't share the same dependentAssembly-tag.
This is what I ended up with:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="" culture="neutral"/>
<codeBase version="3.0.0.0" href="bin\Newtonsoft_Old\Newtonsoft.Json.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<codeBase version="4.5.0.0" href="bin\Newtonsoft.Json.dll" />
</dependentAssembly>
Got the above Error: in Visual Studio 2013
To Fix: In package mamnager Execute: Install-package newtonsoft.json
This will add a new line in packages.config
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />
Remove the previous line which might point to previous version on packages.config.
Delete the old version's directory on the packagers directory.
Remove the reference of NewtonSoft.Json and readd it pointing to the latest version.
Root webconfig will have the following
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
once everything is done. Close and reopen visual studio.
This should fix it.
I had the same error when installing
PM> install-package durandal.starterkit
I used the above method to fix.
I have fixed this issue easily: I had not copied the xml configuration file from the compilation folder.
I just made sure that the xml configuration file was also included along with my program and everything worked fine!
Just had this happen with TeamCity and I imagine others will soon experience this. This probably applies to most build servers that pull NuGet packages.
All the answers that say to do redirects are correct. However, you need to still define the correct version number. My project was using Newtonsoft.Json 7.0, however, they had just released 8.0 and TeamCity was pulling down 8.0 which was causing issues only on the server and not locally. All my redirects were set to 7.0.
Make sure that the deployed application is actually getting the correct version from NuGet and not just the latest and greatest. Or update your config to point to the newest version.
Other solutions didn't work for me. Although I had different nuget package (Newtonsoft.Json.Schema version=3.0.0.0).
So my project was an ASP .NET project and the Newtonsoft.Json.Schama package was referred in a .NET Standard project. The solution was simply to add the Nuget package to the WEB (or startup) project too, and the problem disappeared.

Assembly.LoadFrom how behaves on multi version dll

I've a dll with 2.0.0.1 version in one server which can be downloaded by accessing http://someipaddress/assembly/test.dll and I'm having another application which need to download this test.dll and have to access those methods.
When surfing for this, i've got three different methods to do,
1. Assembly.LoadFrom()
2. Assembly.LoadFile()
3. Assembly.Load()
I've tried Assembly.LoadFrom("http://someipaddress/assembly/test.dll")
Now i've replaced test.dll with 2.0.0.2 version and
What will happen the application download 2.0.0.2 test.dll and already downloaded test.dll 2.0.0.1.
Application which dll will refer?
Will it use existing test.dll 2.0.0.1 since its already downloaded while accessing test.dll 2.0.0.2?
Please suggest on this.
It'll depend on how you're referencing the assembly. By default if there's no binding redirect, your new dll will cause an exception. You can get around this by specifying an binding redirect rule in your application's configuration file like so;
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Test" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.2" newVersion="2.0.0.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
But you'd need to update it upon getting the new version of the dll.

Newtonsoft.json assembly package version mismatch

I am trying to use SocketIO4Net to create socket.io client in .net. Itseems SocketIO4Net has a dependency of Newtonsoft.Json >= 4.0.8. I also am using PushSharp library which has a Newtonsoft.Json dependency of >= 4.5.10. I got NewtonSoft.Json 4.5.11 when i first installed PushSharp and I thought this version should support SocketIO4Net as well since its a higher version but i get this error whenever am trying to connect to socket.io server.
Could not load file or assembly 'Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I have been banging my head all day with these dependency issues, I would be very grateful if someone can point me in the right direction.
Found solution, try with:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
You can modify assembly-binding configuration and add a redirect. See Redirecting Assembly Versions on MSDN.
Basically you want to add following snippet to your app.config or web.config file:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json"
publicKeyToken="30ad4fe6b2a6aeed"
culture="neutral" />
<!--
Assembly versions can be redirected in application,
publisher policy, or machine configuration files.
-->
<bindingRedirect oldVersion="1.0.0.0-4.5.11.0" newVersion="4.5.11.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
EDIT
Why do you need to redirect assembly versions? Even though SocketIO4Net supports newer versions of Newtonsoft.Json, it was compiled against a single version (4.0.8 in your case). This version is stored in the DLL and it is used to load DLLs SocketIO4Net depends on.
Note that NuGet dependencies are not the same as DLL/runtime dependencies - NuGet dependency on Newtonsoft.Json >= 4.0.8 only means that you will be allowed to install SocektIO4Net into a project that has a newer version of Newtonsoft.Json, it has nothing to do with runtime settings.
That being said, recent NuGet versions should add assembly-binding-redirects automatically for you if your project has app.config or web.config file.
The above solutions are correct but there is one more point that should not be forgotten: the app.config content was the same as the above solutions.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
But it's a good idea to check if it's up to date. In my case, Newtonsoft.JSON (v.6.0.4) has come to depend on another package.
There are two option;
Update (Newtonsoft.JSON package) last versions.
Update app.config file in the version numbers.
And last advice, if you are working with more than one project, eg.
exe-dll and check both versions if there is Newtonsoft.JSON.
Put in an assembly redirect in your app/web.config;
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="1.0.0.0-4.5.11.0" newVersion="4.5.11.0" />
</dependentAssembly>
Please note the versions numbers need to match the version you have installed.
Had this same issue.
Just resolved it.
It happened after NuGet was used to install Ext.NET which has a dependency for Newtonsoft.JSON.
There was already a Newtonsoft.JSON.dll file in /bin (and obviously a reference to it in the web.config file) folder without checking I started the NuGet Package-Install procedure while debugging(so the file probably had a lock).
On the runtime error window it will tell you on the stack trace what part of the manifest it has a problem with, mine was major version so I checked the install package version. and it was 1 major version out. Found the original NuGet file under: "[physical path]/../packages/Newtonsoft.Json.[version]/lib/[.net version]/"
Both Manifest and Library was there so copied it into /bin folder, updated the root web.config assembly information and it worked.
Code samples:
Before
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
After
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
Hope this helps
In my case, I removed the package with NuGet and installed a fresh one. Then, remove the reference from References and add again manually. Works like charm. Hope resolve for you.
I was working on an old project recently. I needed to update our Newtonsoft.Json.dll, since I had to utilize a "new" API which required a newer version, but I still had other DLLs that required the old version.
bindingRedirect you say? Nope. It kept complaining about the manifest mismatch.
Separate codeBase tags? Nope. It kept complaining about the manifest mismatch.
The problem was, apparently, that the old version of Newtonsoft.Json.dll (3.0.0.0) does NOT have a PublicKeyToken, but the "new" version (4.5.7.1) DOES have a PublicKeyToken. Therefore they couldn't share the same dependentAssembly-tag.
This is what I ended up with:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="" culture="neutral"/>
<codeBase version="3.0.0.0" href="bin\Newtonsoft_Old\Newtonsoft.Json.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<codeBase version="4.5.0.0" href="bin\Newtonsoft.Json.dll" />
</dependentAssembly>
Got the above Error: in Visual Studio 2013
To Fix: In package mamnager Execute: Install-package newtonsoft.json
This will add a new line in packages.config
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />
Remove the previous line which might point to previous version on packages.config.
Delete the old version's directory on the packagers directory.
Remove the reference of NewtonSoft.Json and readd it pointing to the latest version.
Root webconfig will have the following
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
once everything is done. Close and reopen visual studio.
This should fix it.
I had the same error when installing
PM> install-package durandal.starterkit
I used the above method to fix.
I have fixed this issue easily: I had not copied the xml configuration file from the compilation folder.
I just made sure that the xml configuration file was also included along with my program and everything worked fine!
Just had this happen with TeamCity and I imagine others will soon experience this. This probably applies to most build servers that pull NuGet packages.
All the answers that say to do redirects are correct. However, you need to still define the correct version number. My project was using Newtonsoft.Json 7.0, however, they had just released 8.0 and TeamCity was pulling down 8.0 which was causing issues only on the server and not locally. All my redirects were set to 7.0.
Make sure that the deployed application is actually getting the correct version from NuGet and not just the latest and greatest. Or update your config to point to the newest version.
Other solutions didn't work for me. Although I had different nuget package (Newtonsoft.Json.Schema version=3.0.0.0).
So my project was an ASP .NET project and the Newtonsoft.Json.Schama package was referred in a .NET Standard project. The solution was simply to add the Nuget package to the WEB (or startup) project too, and the problem disappeared.

Assembly Binding Redirect to a lower version

I am trying to downgrade a NServiceBus dependency so instead of using 4.0.0.0 to use 2.5.0.0
I am trying with the following ways, none of which seem to work.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NServiceBus"
publicKeyToken="9fc386479f8a226c" culture="neutral"/>
<bindingRedirect oldVersion="4.0.0.0" newVersion="2.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
I also tried with codebase :
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NServiceBus"
publicKeyToken="9fc386479f8a226c"
culture="neutral"/>
<codeBase version="2.5.0.0" href="NServiceBus.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Still, nada. I went through the msdn documentation and there is no mention of using this capability in a backwards way. Is this possible?
I'm actually using your first statement for some interop DLLs because the clients in our company have a different state regarding office updates. This is the code I use :
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="office" publicKeyToken="71E9BCE111E9429C" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-14.0.0.0" newVersion="11.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
This provides backwards compatibility from version 14 of the DLL to version 11. Could you provide a link to the DLL u are using?
I've downloaded the NServiceBus framework (version 3.3.8) and investigated the DLL with ILSpy. I would suggest you do the same with your DLL. For my DLL it shows me the same public Key token as yours. Are you sure, that you use version 4.0.0.0 and not verision 3.3.0.0. Or you missmatched the public Key tokens perhaps.
According to MSDN: https://msdn.microsoft.com/en-us/library/eftw1fys(v=vs.110).aspx
This value can specify an earlier version than oldVersion.
referring to the newVersion attribute of bindingRedirect. Also in the "Remarks" section:
You can also redirect from a newer version to an older version of the assembly.
Their example is:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<bindingRedirect oldVersion="1.0.0.0"
newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I did notice it also mentions something about Explicit assembly binding redirection in an application configuration file requires a security permission, maybe that's affecting you as well?
The answers above are good answers but they are missing important part. And if you read comments, some devs no longer believe this is working. In fact, this is working and working both ways, up version or down
Important: culture
In my experiment, it started to work only when I set culture to neutral. (examples above have 'en-us')
<assemblyIdentity name="..... culture="neutral"/>
In my setup I have WinApp that references Lib1 and Lib3. Lib1 references Lib2 and Lib3. Lib2 references Lib3. And when I push the button, a call propagated all the way from WinApp to Lib3 (both, direct and through chain of libs), and text return displayed on screen. Lib3 is strong named.
Run 1 - no culture set [assembly: AssemblyCulture("")], no binding redirect set - WORKS!
Run 2 - Lower version in Lib3, set binding redirect to 'en-us' - FAIL!
Run 3 - Lower version in Lib3, set binding redirect to 'neutral' - WORKS! Works up-version and down-version.
On other runs - playing with setting [assembly: AssemblyCulture("en-us")] - all attempts failed when AssemblyCulture was not empty. In fact, when set this attribute in WinApp, it wouldn't even compile
Error CS7059: Executables cannot be satellite assemblies; culture should always be empty
So, here we go. Since I don't understand all intricacies of role of the AssemblyCulture I only claim that my experiment only proves that under specific conditions version redirect works fine.
If I didn't get you wrong I have done the same thing with stimulsoft report DDLs which I had the latest version installed but I wanted 2010.3 in my application. but not through the config file and redirecting:
I simply removed the reference from the solution explorer and added the old DLL refrence, then I set the copy Local property and recompiled so that the DLL would go with application in a same directory, every thing works fine. also done it with some other dlls as well.

Unit testing c# code in a ScriptSharp project

Im using ScriptSharp to create a RIA app.
Works nice besides some oddities.
However finding and fixing problems using Firebug isn't really convinient.
Since scriptsharp also delivers a Dll I was hoping to use a separate testproject using Nunit to test some parts of my code.
Issue that arises is that the generated dll references mscorlib 0.7 resulting in conflict with mscorlib 4 in the test project.
A simple solution is to create a second plain C# project and copy codefiles around. But maintaining 2 projects with the same code base...
Curious if there is another way to do this. Anybody?
EDIT:
Solution as proposed by Christian Dalager works.
Small thing is that ScriptSharp has redefined System.Diagnostics in mscorlib. No more Debug.Assert/Writeline. But there is almost no more need for it now.
You might try using assembly binding redirects
You would put something like this in the app.config on your testproject.
Havent tested this particular configuration, so you will need to adjust it.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
<dependentAssembly>
<assemblyIdentity name="mscorlib" publicKeyToken="b77a5c561934e089" culture="neutral"/>
<bindingRedirect oldVersion="0.7.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

Categories