简体   繁体   English

WCF多个服务之间的共享类型

[英]Share Types between WCF of Multiple Services

I have a Java web server that has 2 endpoints: SystemManagement and UserManagement. 我有一个具有2个端点的Java Web服务器:SystemManagement和UserManagement。 The 2 endpoints use the same libraries. 2个端点使用相同的库。 Therefore, almost all the classes in that two endpoints are identical. 因此,这两个端点中的几乎所有类都是相同的。

And I have a C# client side that uses that 2 services. 我有一个使用这2个服务的C#客户端。 I know that WCF can share classes. 我知道WCF可以共享课程。 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. 但是,当我更新服务引用时,Visual Studio仍会自行生成类“会话”。

[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? 如何使2个服务引用使用公共类? I don't want the 2 service references generating their own class which is duplicated. 我不希望这2个服务引用生成自己的重复的类。

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. 这将迫使Visual Studio反映第一个服务引用的类型,并尝试在可能的情况下引用它们,而不是在其他命名空间中重新创建它们。

在此处输入图片说明

By specifying re-use in this way, visual studio calls svcutil.exe under the hood with the /r flag. 通过以这种方式指定重用,Visual Studio使用/ r标志在幕后调用svcutil.exe。

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. 但是,svcutil.exe使用DataContractSerializer来帮助生成代码,不幸的是,在解析服务合同时,它具有一套相当严格的规则

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). 因此,除非您为XSD服务,否则svcutil.exe将切换为使用XmlSerializer ,该XmlSerializer不支持/ r标志(或重新使用)。 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. 您还可以使用WSCF.blue生成服务合同,因为它具有自己的自定义序列化程序并支持类型的重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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