Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4244074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:39:38+00:00 2026-05-21T03:39:38+00:00

edit: As mentioned in my comment I found out the reason for this problem

  • 0

edit: As mentioned in my comment I found out the reason for this problem was that the Module object has a reference back to the OrderInfo object. DataContractSerializer don’t support preserving the object references by default. I have now been able get all this to work correctly. If anyone is interested contact me and I’ll add it in an answer here.

  • .net service to .net client with shared POCO (data objects) library at both ends.
  • Object OrderInfo contains a List. If the list contains any Module objects I get the dreaded “The underlying connection was closed: The connection was closed unexpectedly.”
  • I can send a “standalone” List from another service method and it works fine so Module object by themselves serialize/deserialize fine.
  • I don’t use datacontract in the POCO classes, WCF handles this automatically (which might also be the problem. I’ve tried adding:

    [Serializable]
    [XmlInclude(typeof(List<Module>))]
    

but that didn’t help. I can’t see what the problem is since I do THE EXACT SAME thing in the Module object returning a collection of Pricemodel objects.

    public class OrderInfo
    {
    int _ProductID;
    IList<Module> _Modules = new List<Module>();
    //IList<MiscProduct> _MiscProduct = new List<MiscProduct>();

    public IList<Module> Modules
    {
        get
        {
            return new List<Module>(_Modules).AsReadOnly();
        }
        set
        {
            _Modules = value;
        }
    }
    }

    public class Module
    {
    string _Name;
    int _Sort_Number;
    string _Description;
    OrderInfo _OrderInfoMaster;
    IList<Pricemodel> _Pricemodels = new List<Pricemodel>();

    public IList<Pricemodel> Pricemodels
    {
        get
        {
            return new List<Pricemodel>(_Pricemodels).AsReadOnly();
        }
        set
        {
            _Pricemodels = value;
        }
    }
    }

Calling client code is:

    using (ProductOrderItems_WCFService.ProductOrderServiceClient client = new ProductOrderItems_WCFService.ProductOrderServiceClient())
    {
        string s = client.HelloWorld();
        Module m = client.GetModule();
        List<Module> mods = client.GetModuleList(7);
        grdModules.DataSource = mods;
        grdModules.DataBind();

        OrderInfo oi = client.GetOrderInfo(7);
    }

It fails on the last line when I request OrderInfo object from service. All the above calls work great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:39:39+00:00Added an answer on May 21, 2026 at 3:39 am

    First the custom DataContactSerializerOperationBehavior

    using System;
    using System.ServiceModel.Description;
    using System.Runtime.Serialization;
    using System.Collections.Generic;
    
    /// <summary>
    /// Summary description for ReferencePreservingDataContractSerializerOperationBehavior
    /// </summary>
    public class ReferencePreservingDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
    {
        public ReferencePreservingDataContractSerializerOperationBehavior(OperationDescription operation) : base(operation)
        {
        }
    
        public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, true, this.DataContractSurrogate);
        }
    
        public override XmlObjectSerializer CreateSerializer(Type type, System.Xml.XmlDictionaryString name, System.Xml.XmlDictionaryString ns, IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, true, this.DataContractSurrogate);
        }
    }
    

    Next is the SelfDescribingServiceHost to allow us to use the ReferencePreservingDataContractSerializerOperationBehavior

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    namespace NewWcfService
    {
        //This class is a custom derivative of ServiceHost
        //that can automatically enabled metadata generation
        //for any service it hosts.
        class SelfDescribingServiceHost : ServiceHost
        {
            public SelfDescribingServiceHost(Type serviceType, params Uri[] baseAddresses)
                : base(serviceType, baseAddresses)
            {
            }
    
            //Overriding ApplyConfiguration() allows us to 
            //alter the ServiceDescription prior to opening
            //the service host. 
            protected override void ApplyConfiguration()
            {
                //First, we call base.ApplyConfiguration()
                //to read any configuration that was provided for
                //the service we're hosting. After this call,
                //this.ServiceDescription describes the service
                //as it was configured.
                base.ApplyConfiguration();
    
                foreach (ServiceEndpoint endpoint in this.Description.Endpoints)
                    SetDataContractSerializerBehavior(endpoint.Contract);
    
                //Now that we've populated the ServiceDescription, we can reach into it
                //and do interesting things (in this case, we'll add an instance of
                //ServiceMetadataBehavior if it's not already there.
                ServiceMetadataBehavior mexBehavior = this.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (mexBehavior == null)
                {
                    mexBehavior = new ServiceMetadataBehavior();
                    this.Description.Behaviors.Add(mexBehavior);
                }
                else
                {
                    //Metadata behavior has already been configured, 
                    //so we don't have any work to do.
                    return;
                }
    
                //Add a metadata endpoint at each base address
                //using the "/mex" addressing convention
                foreach (Uri baseAddress in this.BaseAddresses)
                {
                    if (baseAddress.Scheme == Uri.UriSchemeHttp)
                    {
                        mexBehavior.HttpGetEnabled = true;
                        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                                MetadataExchangeBindings.CreateMexHttpBinding(),
                                                "mex");
                    }
                    else if (baseAddress.Scheme == Uri.UriSchemeHttps)
                    {
                        mexBehavior.HttpsGetEnabled = true;
                        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                                MetadataExchangeBindings.CreateMexHttpsBinding(),
                                                "mex");
                    }
                    else if (baseAddress.Scheme == Uri.UriSchemeNetPipe)
                    {
                        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                                MetadataExchangeBindings.CreateMexNamedPipeBinding(),
                                                "mex");
                    }
                    else if (baseAddress.Scheme == Uri.UriSchemeNetTcp)
                    {
                        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                                MetadataExchangeBindings.CreateMexTcpBinding(),
                                                "mex");
                    }
                }
    
            }
    
            private static void SetDataContractSerializerBehavior(ContractDescription contractDescription)
            {
                foreach (OperationDescription operation in contractDescription.Operations)
                {
                    DataContractSerializerOperationBehavior dcsob = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
                    if (dcsob != null)
                    {
                        operation.Behaviors.Remove(dcsob);
                    }
                    operation.Behaviors.Add(new ReferencePreservingDataContractSerializerOperationBehavior(operation));
                }
            }
        }
    }
    

    Then there is the ServiceHostFactory:

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    
    namespace NewWcfService
    {
            public class SelfDescribingServiceHostFactory : ServiceHostFactory
        {
            protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                //All the custom factory does is return a new instance
                //of our custom host class. The bulk of the custom logic should
                //live in the custom host (as opposed to the factory) for maximum
                //reuse value.
                return new SelfDescribingServiceHost(serviceType, baseAddresses);
            }
        }
    }
    

    And of course the Service.svc to use the new HostFactory:
    <%@ ServiceHost Language="C#" Debug="true" Service="NewWcfService.Service" Factory="ProTeriaWCF.SelfDescribingServiceHostFactory" CodeBehind="Service.svc.cs" %>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: From another question I provided an answer that has links to a lot
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: This question is more about language engineering than C++ itself. I used C++
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Edit: I have solved this by myself. See my answer below I have set
Edit: There is a somewhat strange solution to this question. Check my answer posted
Whenever I exit my program it gives me this exception 0xC0000022: A process has
EDIT What small things which are too easy to overlook do I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.