c# - Using Interfaces With WCF -


i have googled , read hours , can't find deals specific scenario...

i want use interfaces in wcf service contracts loosely couple service classes used on each end of wire. enable have low-level assembly contains service , data contracts (just interfaces) can hand consultant. on end of wire can instantiate data classes implement our data contract interface, send on wire us, , our wcf service translate/cast/whatever incoming data our version of data class implements same interface.

here's example. idatacontract contains bare information want transmit on wire. endpoints , other wcf-specific config default stuff (my problems may lie in that, can include more of if that's need change things).

edit: i've included more of code , renamed couple classes less confusing. name & namespace additions datacontractattributes, 2 sections in config files new additions based on information this blog post. if switch abstract base class instead of interface, it works. however, i'd working interface if possible.

shared library (my code, shared client authors):

public interface idatacontract {     string myproperty { get; set; } } [servicecontract] public interface itestservice {     [operationcontract]     idatacontract testsharedinterface(idatacontract clientdata); } 

client code (their's):

[datacontract(name = "idatacontract", namespace = "http://services.sliderhouserules.com")] public class clientdataclass : idatacontract {     [datamember]     public string myproperty { get; set; } } private static void calltestsharedinterface() {     endpointaddress address = new endpointaddress("http://localhost/servicecontractstest.wcfservice/testservice.svc");     channelfactory<itestservice> factory = new channelfactory<itestservice>("itestservice", address);     itestservice proxy = factory.createchannel();     ((iclientchannel)proxy).open();      idatacontract clientdata = new clientdataclass() { myproperty = "client data" };     idatacontract serverdata = proxy.testsharedinterface(clientdata);     messagebox.show(serverdata.myproperty); } 

client config:

<system.runtime.serialization>     <datacontractserializer>         <declaredtypes>             <add type="servicecontractstest.contracts.datacontracts.idatacontract, servicecontractstest.contracts">                 <knowntype type="servicecontractstest.wcfclient.clientdataclass, servicecontractstest.wcfclient"/>             </add>         </declaredtypes>     </datacontractserializer> </system.runtime.serialization> 

server code (mine):

public class testservice : itestservice {     public idatacontract testsharedinterface(idatacontract clientdata)     {         serverdataclass convertedclientdata = (serverdataclass)clientdata;         idatacontract serverdata = new serverdataclass() { myproperty = convertedclientdata.myproperty + " + server data added" };         return serverdata;     } } [datacontract(name = "idatacontract", namespace = "http://services.sliderhouserules.com")] public class serverdataclass : idatacontract {     [datamember]     public string myproperty { get; set; } } 

server config:

<system.runtime.serialization>     <datacontractserializer>         <declaredtypes>             <add type="servicecontractstest.contracts.datacontracts.idatacontract, servicecontractstest.contracts">                 <knowntype type="servicecontractstest.wcfservice.serverdataclass, servicecontractstest.wcfservice"/>             </add>         </declaredtypes>     </datacontractserializer> </system.runtime.serialization> 

i getting serialization error on client call complaining known types. missing metadata markup in client class? i'm @ loss know problem lies, i've tried searches can think of , no 1 seems have dealt specific scenario.

basically, want clientdataclass serialize <idatacontract><myproperty>client data</myproperty></idatacontract> , able deserialize serverdataclass instance. seems should possible.

if data contracts interfaces wcf can't know object instantiate incoming request. there no need class same in service, after add service reference reads wsdl , generates new classes based on type info in wsdl.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -