Adding a WCF Service to a project - c#

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).

Related

Load AppConfig with binding redirects from resource

I have a .NET Framework 4.6.2 application myApp.exe with several internal and external dependencies. All dependencies are included into the main application myApp.exe as resources.
On application startup, our custom assembly loader gets notified via the event AssemblyResolve and loads the requested assembly from myApp.exe's resources via GetManifestResourceStream() and Assembly.Load().
This works fine, except App.config: the resources contain myApp.App.config, but the custom assembly loader is not loading it yet. This leads to a problem because all the assembly binding redirects specified in App.config are not applied by the .NET Runtime. When placing myApp.exe.config beside myApp.exe (as usual in .NET), everything is working fine.
I'm trying to find a way to avoid the additional file myApp.exe.config. Instead, I want to load the app config (especially the binding redirects) from the resources as well. Is that even possible? If yes, how can I do so?
Unfortunatelly, you can't tell CLR which file you want to load as a runtime config. Runtime looks up for xml configuration in following order:
application config
machine config
The lookup is performed every time when CLR needs info from config, for example binding redirects and assembly locations on assembly load.
It is mostly a part of assembly loading process and it is better to leave as is. You can read more about it here: https://learn.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies
Edit: However, you can live without config file. You said you have all your assemblies embedded into exe assembly and you load everything via custom resolver. Note that your custom assembly resolver is called only when standard resolve pipeline fails to load an assembly. Take a look at this answer: https://stackoverflow.com/a/26673659/2811109

NHibernate & NET Persistence API not working in test project

I am getting following error while creating EntityManager in my Data Access Layer(DAL) (Class Library project):
File name: 'Iesi.Collections, Version=1.0.1.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4' ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
I am getting this error from my TestProject (Class Library project) where I have return NUnit test cases.
I have one web application which also calls DAL, but during this call it is able to create EntityManager properly.
Actually I need to add following lines to config files which avoids the above error:
<runtime>
<loadFromRemoteSources enabled="true" />
</runtime>
Now the problem is, I have obviously added these lines to my web.config, and that is I am able to create EntityManager. But how can I add this lines to my TestProject (which is a Console Library), because it will not have a config file associated with it.
PS: I am using NHibernate and NET Persistence API.
You have to copy the Iesi.Collections in the bin directory, the easiest way is to add a reference in your test project.

WCF Service in Sharepoint 2010 can't find my Custom ServiceHostFactory class

I made a great WCF service in my dev environment, then when I pushed it up to production I got various errors saying "This collection already contains an address with scheme http..."
So, I created a Custom ServiceHostFactory class for my service.
Now when I upload it I get:
The CLR Type 'WCFServices.MultipleHostFactory' could not be loaded during
service compilation. Verify that this type is either defined in a source
file located in the application's \App_Code directory, contained in a
compiled assembly located in the application's \bin directory, or present in
an assembly installed in the Global Assembly Cache. Note that the type name
is case-sensitive and that the directories such as \App_Code and \bin must
be located in the application's root directory and cannot be nested in
subdirectories.
I checked the GAC and my assembly is indeed there.
Can anyone explain why this error is occuring?
Again, I am using .NET 3.5 on Sharepoint 2010
Is it "WCFServices.MultipleHostFactory" or "WCFServices.MultipleHostsFactory"? If its the first, thats why because that namespace does not match that you said in your question.
It turns out that I was able to remove one of the bindings from the application host configuration file. After talking with the administrator, I found that he had two bindings for port 80. One was IP:80:[DNS name] and the other was :80:
We agreed to get rid of the :80: binding and the WCF service was able to deploy to SP.
If anyone ever runs into that CLR loading issue, please post with a solution. I still don't have a good explanation for that problem.

External component has thrown an exception from third party assembly

We use a 3rd party mixed mode assembly that interfaces with a printer device.
In one of our applications, the assembly functions correctly.
In another application, it was generating the following exception:
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.
After doing some research, I discovered that I could add the following to the app.config file to resolve this problem:
<startup useLegacyV2RuntimeActivationPolicy="true">
(The application that works already had this setting in its app.config)
Although this did resolve the "Mixed mode assembly" issue, now I'm seeing a somewhat different problem:
External component has thrown an exception
The strange thing is that I have one application which works correctly and another application which does not. They both call the third party assembly via the same code (which is another one of our assemblies).
I tried creating a very simple test app which calls the third party assembly using the same code path, and it also fails with the "External component has thrown an exception" error.
I've compared the app.config files for all of the applications and they are essentially the same, and I've double checked that all of the build settings are the same for each. Both projects target .NET 4.0 and X86.
There is obviously something different about the two applications but I'm at a loss to identify what it is.
Development environment is Visual Studio 2010.
Does anyone have any suggestions on what other areas I could investigate?
#tsells was right: there was a missing dependency
The third party assembly in question depended on another DLL that was present in the first applications bin folder.
I copied all of the files from the working application's bin folder to the non-working application's bin folder, and it resolved the problem.
From there it was just a matter of deducing which DLL was missing and including it in the second applications project.
When there are dependencies missing, you can use tools like CheckAsm for managed and Dependency Walker for unmanaged libraries to check for dependencies.

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).

Categories

Resources