WCF Serialization Exception

454 views
Skip to first unread message

ultraviolet

unread,
Dec 12, 2012, 2:48:17 PM12/12/12
to agath...@googlegroups.com
Hello Everyone,
I have successfully completed Davy's HelloWorld example and everything works fine.
Now, I'm trying to create my host as a WCF Service Application and I receive the following error on my client

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:requests. The InnerException message was 'Error in line 1 position 370. Element 'http://schemas.datacontract.org/2004/07/Agatha.Common:Request' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/Sample.Common.RequestsAndResponses:HelloWorldRequest'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'HelloWorldRequest' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

 I believe the issue is that container is not getting initialized on the host. If I go back to the original HelloWord example that uses an ASP.NET Web Application and comment out the ComponentRegistration.Register(); from the Application_Start() I can recreate the error. ComponentRegistration.Register() contains:

new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(HelloWorldRequest).Assembly,
                typeof(Agatha.Castle.Container)).Initialize();

So, my question is how do I get this to register in the WCF Service Application?

Thanks for your help,
Dave

 

ultraviolet

unread,
Dec 14, 2012, 4:15:09 PM12/14/12
to agath...@googlegroups.com
I've tried a couple different approaches and I think I'm getting closer. Here is my question now (posted from stackoverflow):

I have developed an WCF Service Library that uses Agatha RRSL, but I can not figure out how to initialize the container. If I recreate this service in an ASP.NET Web Application, I can call the initialization code from the Global.asax.cs Application_Start() and everything works perfectly. The initialization code is:

public static class ComponentRegistration
{
    public static void Register()
    {
      new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(),
      typeof(HelloWorldRequest).Assembly,
      typeof(Agatha.Castle.Container)).Initialize();
    }
}

In the WCF Service Library, I added an App_Code folder with a class that calls:

public static void AppInitialize()
{
    ComponentRegistration.Register();
}

That didn't work as my client app throws an exception that there is no response with that type. I also tried adding a component to the web.config file, but I never got that even close to working.

I also tried to create a custom ServiceHost that does the initialization:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Agatha.ServiceLayer;
using System.Reflection;
using Sample.Common.RequestsAndResponses;

namespace Sample.ServiceLayer.WCFHost
{
    public class CustomServiceHostFactory : ServiceHostFactory
    {
        public CustomServiceHostFactory()
        {
            new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(HelloWorldRequest).Assembly,
                typeof(Agatha.Castle.Container)).Initialize();
        }

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new CustomServiceHost(serviceType, baseAddresses);
        }
    }

    public class CustomServiceHost : ServiceHost
    {
        public CustomServiceHost()
        {
        }

        public CustomServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        protected override void OnOpening()
        {
            base.OnOpening();
        }

        protected override void OnClosing()
        {
            base.OnClosing();
        }

        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();
        }
    }
}

However, I get the same exception on my client:

System.InvalidOperationException was unhandled
  Message=There is no response with type Sample.Common.RequestsAndResponses.HelloWorldResponse. Maybe you called Clear before or forgot to add appropriate request first.
  Source=Agatha.Common
  StackTrace:
       at Agatha.Common.RequestDispatcher.Get[TResponse]() in c:\src\Agatha\Agatha.Common\RequestDispatcher.cs:line 125
       at Agatha.Common.RequestDispatcher.Get[TResponse](Request request) in c:\src\Agatha\Agatha.Common\RequestDispatcher.cs:line 150
       at ConsoleApplication1.Program.Main(String[] args) in C:\Users\ultraviolet\Documents\Visual Studio 2010\Projects\AgathaHelloWorld\ConsoleApplication1\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

What approach should I take to get the WCF Service Library to run my initialization code so that the host returns the correct type? Any guidance would be much appreciated. Thanks.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages