0

A custom type (e.g. Engine) is defined in two different namespaces on WCF server side, which is exposed to WCF client as Engine, Engine1. How to set up so that the exposed types have the same name, Engine in this case.

Below is my example code:

namespace WcfServiceLibrary1
{
    [ServiceContract]
    interface ICar
    {
        [OperationContract]
        void RepairMotorCycle(MotorCycle motorCycle);

        [OperationContract]
        void RepairTwoDoorCar(TwoDoorCar Car);
    }


    public class Car:ICar
    {

        public void RepairMotorCycle(MotorCycle motorCycle)
        {
            throw new NotImplementedException();
        }

        public void RepairTwoDoorCar(TwoDoorCar Car)
        {
            throw new NotImplementedException();
        }
    }
}

namespace WcfServiceLibrary1.MC
{

    public class MotorCycle
    {
        public Engine Engine { get; set; }
    }

    public class Engine { }

}

namespace WcfServiceLibrary1.C
{
    public class TwoDoorCar
    {
        public Engine Engine { get; set; }
    }

    public class Engine { }
}

Below is the WCF client for Engine:

 [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="Engine", Namespace="http://schemas.datacontract.org/2004/07/WcfServiceLibrary1.MC")]
    [System.SerializableAttribute()]
    public partial class Engine : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

}

 [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="Engine", Namespace="http://schemas.datacontract.org/2004/07/WcfServiceLibrary1.C")]
    [System.SerializableAttribute()]
    public partial class Engine1 : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    }

Please note that both MotoCycle and TwoDoorCar contain a large number of custom type that have the same name but different function. Thus, it is tedious to change the name on client side (e.g. change Engine1 to Engine for all occurences). Also it is tedious to solve it by using class inheritance. It is ok to define two custom types that have the same name, which might need less work.

Any idea would be very much appreciated!

Edit *Possible Solution*

Put it into separate interface, as below

[ServiceContract]
        interface ICar1
        {
            [OperationContract]
            void RepairMotorCycle(MotorCycle motorCycle);
    }


 [ServiceContract]
        interface ICar2
        {
            [OperationContract]
            void RepairTwoDoorCar(TwoDoorCar Car);
        }

This will put the same custom type in different namespace on client side.

Pingpong
  • 7,681
  • 21
  • 83
  • 209

2 Answers2

1

If your Engines represent an identical concept, you could define one Engine in a dedicated namespace and reference it from WcfServiceLibrary1.MCand WcfServiceLibrary1.C.

Your example however suggests that you should rather gather your vehicles into a single namespace and make use of inheritance.

namespace WcfServiceLibrary.Vehicles
{
    public class Engine
    {
    }

    public abstract class Vehicle
    {
        public Engine { get; set; }
    }

    public class Car : Vehicle
    {
    }

    pulic class Motorcycle : Vehicle
    {
    }
}

Moving your Engine to a common namespace could look like this:

namespace WcfServiceLibrary.Common
{
    public class Engine
    {
    }
}

Your "Motorcycle" library

using WcfServiceLibrary.Common

namespace WcfServiceLibrary.MC
{
    public class Motorcycle
    {
        public Engine Engine { get; set; }
    }
}

... and your "Car" library

using WcfServiceLibrary.Common

namespace WcfServiceLibrary.C
{
    public class Car
    {
        public Engine Engine { get; set; }
    }
}

You won't have to change your Engine property.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Thanks! Due to the current implementation, this option is impossible. – Pingpong Aug 08 '11 at 12:37
  • Even moving `Engine` to a referenced namespace? – Filburt Aug 08 '11 at 13:11
  • Filburt, thank you for your advice. The idea of sharing the custom type (e.g. Engine) is NOT suitable, because different function will be assigned to the same type. e.g. Engine type has completely different function. – Pingpong Aug 08 '11 at 13:49
  • I don't think I can follow you any more: In your question you said you could replace all occurences of `Engine1` with `Engine` which would mean `WcfServiceLibrary.MC.Engine` and `WcfServiceLibrary.C.Engine` are basically the same type - now you state they are different. Maybe you could update your example to reflect the real complexity of your actual project. – Filburt Aug 08 '11 at 14:18
0

First of all, try and share your code libraries between the server and client. This link will tell you how to do it for Silverlight, if you are not using Silverlight then check this SO search link for a variety of posts and answers on the subject.

Secondly, if you cannot share the libraries then editing the generated client class files will work (just delete the definition of Engine1 and fix up any references to it to point to the Engine), although you will lose the changes if you regenerate the proxy.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • slugster, thanks. Due to the large number of class, this option might be the last resort – Pingpong Aug 08 '11 at 12:38
  • @slugster Fixing a problem resulting from a design that seriously needs refactoring by modifying generated code on the client side seems very dangerous to me. – Filburt Aug 08 '11 at 13:39