c# - Share Types between WCF of Multiple Services -
i have java web server has 2 endpoints: systemmanagement , usermanagement. 2 endpoints use same libraries. therefore, classes in 2 endpoints identical.
and have c# client side uses 2 services. know wcf can share classes. make new project , let client project reference new project. make common class "session" in 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 { { 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 { { 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 update service references, visual studio still generates class "session" 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 { { 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 { { 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 make 2 service references use common class? don't want 2 service references generating own class duplicated.
you can try reuse types referenced assemblies option when generate second service reference. force visual studio reflect on first service reference's types , try reference them possible rather re-creating them in different namespace.
by specifying re-use in way, visual studio calls svcutil.exe under hood /r flag.
however, svcutil.exe uses datacontractserializer
generate code , unfortunately has rather strict set of rules when comes parsing service contracts.
so unless service xsds adhere set of rules svcutil.exe switch use xmlserializer
, doesn't support /r flag (or re-use). hence not able re-use types.
you can use wscf.blue generate service contracts, has it's own custom serializer , supports re-use of types.
Comments
Post a Comment