简体   繁体   中英

Share Types between WCF of Multiple Services

I have a Java web server that has 2 endpoints: SystemManagement and UserManagement. The 2 endpoints use the same libraries. Therefore, almost all the classes in that two endpoints are identical.

And I have a C# client side that uses that 2 services. I know that WCF can share classes. So I make a new project and let my client project reference to the new project. Then make a common class "session" in the new project.

namespace WcfExplore.UserManagement
{
    [DataContract]
    public partial class session : object, System.ComponentModel.INotifyPropertyChanged
    {
        private string sessionIdField;
        private string useridField;

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 3)]
        [DataMember]
        public string sessionId
        {
            get { return this.sessionIdField; }
            set
            {
                this.sessionIdField = value;
                this.RaisePropertyChanged("sessionId");
            }
        }
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 4)]
        [DataMember]
        public string userid
        {
            get { return this.useridField; }
            set
            {
                this.useridField = value;
                this.RaisePropertyChanged("userid");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if (propertyChanged != null)
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

But when I update the service references, the visual studio still generates the class "session" by its own.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://iboss2.service.iasia.com/")]
public partial class session : object, System.ComponentModel.INotifyPropertyChanged {

    private string sessionIdField;

    private string useridField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
    public string sessionId {
        get {
            return this.sessionIdField;
        }
        set {
            this.sessionIdField = value;
            this.RaisePropertyChanged("sessionId");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=4)]
    public string userid {
        get {
            return this.useridField;
        }
        set {
            this.useridField = value;
            this.RaisePropertyChanged("userid");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

What to do to make the 2 service references use the common class? I don't want the 2 service references generating their own class which is duplicated.

You can try the Reuse types from referenced assemblies option when you generate the second service reference. This will force visual studio to reflect over the first service reference's types and try to reference them where possible rather than re-creating them in a different namespace.

在此处输入图片说明

By specifying re-use in this way, visual studio calls svcutil.exe under the hood with the /r flag.

However, svcutil.exe uses DataContractSerializer to help generate code and unfortunately this has a rather strict set of rules when it comes to parsing your service contracts.

So unless you service XSDs adhere to this set of rules svcutil.exe will switch to use the XmlSerializer , which doesn't support the /r flag (or re-use). Hence you will not be able to re-use types.

You can also use WSCF.blue to generate your service contracts, as this has it's own custom serializer and supports re-use of types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM