多业务的WCF之间共享类型 [英] Share Types between WCF of Multiple Services

查看:104
本文介绍了多业务的WCF之间共享类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个端点一个Java Web服务器:SystemManagement和UserManagement。 2.终端使用相同的库。因此,几乎所有类中的两个端点是一致的。

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.

和我有一个使用了2服务C#的客户端。我知道,WCF能够共享类。所以,我提出一个新的项目,让我的客户项目参照新的项目。然后做一个通用类会话的新项目。

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));
        }
    }
}

但是,当我更新服务引用,在Visual Studio还是由它自己生成的类会话。

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));
        }
    }
}

怎么做才能让2服务引用使用通用类?我不希望在2服务引用生成自己的类,它是重复的。

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.

通过指定再利用这种方式,视觉工作室呼叫与/ R标志在引擎盖下svcutil.exe的。

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

不过,svcutil.exe的用途的DataContractSerializer 来帮助生成code,不幸的是这有相当的严格的一套规则的,当涉及到解析您的服务合同。

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.

所以,除非你的服务的XSD遵守这套规则svcutil.exe的将切换到使用的XmlSerializer ,它不支持/ R标志(或重新使用)。因此,你将不能再使用的类型。

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.

您也可以使用 WSCF.blue 生成您的服务合同,因为这有它自己的自定义序列,并支持重复使用的类型。

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.

这篇关于多业务的WCF之间共享类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆