Web service once deployed needs a dependency not used in Visual Studio - c#

I have a web service that once deployed mysteriously needs other DLLs to work.
I have a website running in C:\inetpub\wwwroot\MyWebsite and the web service running in C:\inetpub\wwwroot\MyWebsite\WebService
I created an application (WebService, pointing to C:\inetpub\wwwroot\MyWebsite\WebService) in IIS under the website MyWebsite
When running one of WebService's .svc, I get:
Could not load file or assembly 'Blah.Web' or one of its dependencies. The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'Blah.Web' or one of its dependencies. The system cannot find the file specified.
Blah.web is a DLL used for MyWebsite. Why do suddenly my WebService needs this DLL that's not even referenced in VS 2010 ?
Is it a web.config thing ?
Thanks,

It's because your web service is nested within your website, so when your website automatically compiles, it's compiling the code in your main website AND the web service website. However, since the compiler is functioning undert he context of the main website, it's looking in mywebsite\bin for the code instead of \mywebsite\myservice\bin.
Personally, I'd change the architecture and have the web services site separate, and not nested. it should be, at a minimum, its own web Applicaiton (as configured in IIS) or a separate web site.

Related

Permissions to load assembly at runtime in ASP.NET MVC

I'm currently working on a function for my website that should work like this:
User upload an dll
I load the dll inside another app domain (with Assembly.LoadFrom) to inspect it with reflection
Unload the app domain and delete the .dll file
This works perfectly fine when I do it locally but when I upload it to my server I get:
Failed to generate files: Could not load file or assembly '/the/path' or one
of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
So the "Operation is not supported" make me a bit curious. Is it some kind of permission that I need to set the server? Or is it something I can fix through my config? Or is it simply that my server actually don't support it at all?
The only information I can see on my Windows/IIS server is that it use .NET version 4.
Set project Property Local copy = true ,if you are loading using reflection then provide deployment Item at Class level it will work.

Exception occurring while connecting a C# client application to XSockets Server

I am using XSockets for having two way (web socket based) communication between an XSockets Server and an existing C# desktop client application.
I have integrated code in my existing client application for communicating with XSockets Server.
For now, XSocket Server starts as a separate console application. It starts fine with no problems.
Then I am using following pseudo code block in my C# client application:
XSocketClient client = new XSocketClient("ws://127.0.0.1:4509/MyController", "*", false); //Error occurs right on execution of this line
client.Open();
Following is the Exception snapshot that throws out;
Exception of type 'XSockets.Plugin.Framework.Exceptions.ExportException' was thrown
Custom Message: Failed to load exported interfaces in assembly Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
Source: XSockets.Plugin.Framework
Stack Trace: at XSockets.Plugin.Framework.Composable.SatifyImportsExports()
at XSockets.Plugin.Framework.Composable.Initialize()
at XSockets.Plugin.Framework.Composable.GetExport[T]()
Type: System.IO.FileNotFoundException
Note: If I create a new C# windows form client project and try executing this code, it executes fine and connection is successfully made to the XSocket Server.
Means there is something disturbing from inside my existing client project.
What could the possible reason for this weird exception ? Any ideas? Thanks
Edit: I have noted that if I manually remove Microsoft.Practices.EnterpriseLibrary.Validation DLL from my Bin folder, it raises same kind of exception but with a different custom message this time. I mean then the error is related to some other DLL (of my existing application) present in Bin folder.
I ran into a similar problem yesterday and solved it like so:
Composable.ClearPluginFilters();
Composable.AddPluginFilter("XSockets.*.dll");
This code was called just before I bootstrapped my application and started my server. I know the question is kinda old but if anyone else end up here aswell.
Source from Google Groups
Ok found answer to my own raised question.
There can be two things related to this issue:
Microsoft.Practices.EnterpriseLibrary.Validation.dll was causing problem from the Bin => Release/Debug folders. From Visual studio project references list, right clicked the Microsoft.Practices.EnterpriseLibrary.Validation reference, clicked on Properties and set [Copy Local] property to false. XSocketsClient was now connecting successfully to the XSockets Server but some other logic of my existing application was still needing Enterprise Library which being Copy Local ==> false, was not coming available to the .Net Runtime, so it showed an Exception whenever Runtime was coming across those parts of code.
To resolve the later issue encountered because of removal of Enterprise Library reference, I added again Microsoft.Practices.EnterpriseLibrary.Validation reference and also added one another DLL Microsoft.Practices.EnterpriseLibrary.Common.dll and added its reference to my client application.
Now my client application connects fine to XSocket Server and also now no issue comes with the inclusion of Enterprise Library reference.
Seems like there are some things essential needed by Enterprise Library at runtime which lie in that second DLL.

SOAP/WCF client accessing a service that uses AWSSDK.dll (Amazon S3)

I'm encountering something I did not expect. I have a C# client that accesses a SOAP/WCF service, and the SOAP/WCF service is (trying to) use the AWSSDK.dll to get/put/enumerate objects stored in Amazon S3. Odd thing is, for some reason, the client is getting a FaultException`1 was unhandled with the error text:
Could not load file or assembly 'AWSSDK, Version=1.3.19.0, Culture=neutral, PublicKeyToken=cd2d24cd2bace800' or one of its dependencies. The system cannot find the file specified.
Any idea why in the world the client would need to have the DLL loaded? The server hosting the service has it loaded.
In any case, I added the DLL to the client (I have no clue why I would need to, but just for sanity's sake), and lo and behold, even with the DLL added as a reference, I get the exact same error.
Fault exceptions are generated from the service side so that indicates to me that your service is having the problem with the AWSSDK.dll reference. Make sure that your service has the reference to the file and that when you rebuild the dll is in the bin folder of the service startup project.
To build navigate to menu Build -> Batch Build.. -> Select All -> Rebuild.
You should not have to copy the file manually as the build process should copy it to the proper location if you have it referenced.
Another thing would be to check that you are building under the correct configuration. If the dll is x64 and you are running x86 that might also produce the above error (not exactly sure though).

Adding a WCF Service to a project

I have a C# WinForms Project which contains some WCF service definition files which I have created in the project by adding standard classes (not using Add Item > WCF Service).
The project contains some dependencies that require me to build the project for x86 processors.
If I edit the app.config file with the WCF Service Configuration Editor and try to use the "Create New Service ..." wizard, and then browse to the project EXE file (in the debug folder) I get an error, (which I've read is because I'm targeting the x86 processor):
Could not load file or assembly 'EXE_FILE_NAME' or one of its dependencies. An attempt was made to load a program with an incorrect format.
So, upon changing the target to All CPUs, compiling the project again, and then trying to create the service in the WCF Service Configuration Editor again, I now get a different error:
Could not load file or assembly 'SOLUTION_NAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
(To clarify, I CAN still compile the project when targeting all CPUs, but get an error at runtime due to a dependency)
Is anybody able to help me with this problem, so I can add the services defined in app.config file using the wizards in WCF Service Configuration Editor? (I think that if I add the services to the WCF Service Configuration Editor when targeting All CPUs I can make any modifications thereafter, regardless of the target)
When debugging, look at the exception detail. From what you've described my guess is you will an assembly loading error in the fusion log because the dependencies of the WCF service can't be satisfied by looking in the default locations.
Say for example you're referencing MrCritter.MyService.dll which defines a WCF service and has a dependency on something like log4net or nHibernate. As long as nothing from those dependencies is leaking into the WCF host (eg returning an ILog) yes you'll be able to compile fine but will get ReflectionTypeLoadException thrown when trying to instantiate the service class if those dependencies aren't somewhere it can find (eg in the executing directory, in GAC etc).

Is log4net killing my WCF unit tests?

I have three projects in my solution:
A WCF web service which provides functionality I want to test
A Web Application which calls that web service
A test project which runs tests on the service.
The web service and the web application both use log4net with separate configuration files and this line in the AssemblyInfo.cs for configuration:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
When I browse to the URL of the web service (http://localhost/MyWebService/MyWebService.svc), It appears as expected - information and a link to the wsdl.
When I use the web application, it all works correctly. The code calls the web service and gets correct responses in reply. Logging occurs from both the web app and the web service.
However, when I run my unit tests, they all fail with the following exception:
Test method MyServiceTest.MyServiceAuthTest.TestValidateCorrectly threw exception: System.ServiceModel.ServiceActivationException: The requested service, 'http://localhost/MyWebService/MyWebService.svc' could not be activated. See the server's diagnostic trace logs for more information.
In the Event logs for my local machine, I get the following message:
WebHost failed to process a request.
Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/12905972
Exception: System.ServiceModel.ServiceActivationException: The service '/MyWebService/MyWebService.svc' cannot be activated due to an exception during compilation. The exception message is: Could not load file or assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' or one of its dependencies. Strong name signature could not be verified. The assembly may have been tampered with, or it was delay signed but not fully signed with the correct private key. (Exception from HRESULT: 0x80131045).
I've removed and replaced the references, cleaned and rebuilt, and even purged the temporary asp.net files, all to no avail. The web site calls the service with no problems, but the tests fail.
Does anyone know what could be happening here?
Update: I removed all references to log4net and the tests ran and succeeded without a problem. Obviously, this is far from preferable. Any suggestions?
Update 2: Some combination of these two things fixed the problem:
Added a reference to log4net in my test project, making sure to initialise it completely.
Used the release build of log4net 1.2.10 rather than the debug build.
Do you have a reference from your unit test to the log4net library? Try it out.
The reason behind this: Most unit testing frameworks are shadowing the binaries to somewhere else during the test run, but by doing this they do not find deeply nested references in all cases. I once met this with NHibernate and log4net. In case of MSTest you cannot deactivate shadowing. NUnit GUI has an option for this, but I don't have it installed, so you have to look for it on your own :-)
I would also consider to add log4net handling into your unit tests configuration. This is very helpful when looking at failures anyway.
This link explains it for NUNit and it should be possible for other frameworks as well.

Categories

Resources