3
votes

When I create a WCF Service Library to target the .NET 3.5 framework in Visual Studio 2010 SP1, the WCF Service Host loads the .NET 3.5 assembly in the .NET framework 4.0.30319.237 runtime when debugging.

Since I'm referencing the SharePoint 2010 Server Object Model, I cannot have my code loaded into the .NET 4 runtime, SharePoint assemblies check the runtime version and throw an exception in this case.

The WCF Service Host must be started in the .NET runtime version 2.0.50727.5446 (which is the same runtime for .NET 3.5). Has anyone resolved this?

2
How are you hosting the WCF service ? Is your host project set to run in .net 4.0 ?Kiran Mothe
Hi Kiran, the WCF Service is hosted automatically by VS 2010 via the 'WCF Service Host (wcfsvchost.exe)' when you debug a WCF Service Library project. This host is running in .Net Framework 4 by default, which is why I'm having this issue.Banter
Ah, ok. WcfSvcHost is built with .net 4.0 so it cannot run in 2.0. When it starts up and uses reflection to load the service library, the service library assembly is loaded with the same runtime (4.0) as well. AFAIK, there is no way to make wcfsvchost run in 2.0. It's easier to just create a custom host.Kiran Mothe
OK, I was hoping there was a switch or maybe an alternate service host executable we could use, for example, redirect to the version of wcfsvchost.exe that shipped with VS 2008.Banter

2 Answers

0
votes

You can change some of the configuration of the WcfSvcHost to be able to run your assemlies, without the exception:

   <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" /> 
   </startup> 

You need to add this to the config file located at: C:\Program Files (x86)\Microsoft Visual Studio \10.0\Common7\IDE\WcfSvcHost.exe.config.

For a full detail of this solution, browse to: http://blogs.claritycon.com/bryandougherty/2011/05/24/handling-mixed-mode-assembly-error-in-wcf-service-host/

Please, let me know if this works for you.

0
votes

Faced with the same problem here, the answer for us was to write a separate ConsoleHost application with the .NET 3.5 framework targeted:

using System;
using System.ServiceModel;

namespace Project.Services.ConsoleHost
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(typeof(ProjectServiceManager)))
            {
                // Open the ServiceHost to start listening for messages. Since
                // no endpoints are explicitly configured, the runtime will create
                // one endpoint per base address for each service contract implemented
                // by the service.
                host.Open();

                Console.WriteLine("The Project WCF Services are hosted at:");
                Console.WriteLine();
                foreach (var address in host.BaseAddresses)
                    Console.WriteLine(string.Format("\t{0}", address.ToString()));
                Console.WriteLine();
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }
        }
    }
}