Domanda

Ciao ho una libreria di classi che che esegue i metodi e ha un sacco di diverse classi che usa come parametri per le chiamate metodi ... Sto creando un wrapper WCF per questa libreria di classi. ma io non ho il permesso di modificare la libreria di classi.

Ora la mia domanda è come posso esporre queste classi come contratti dati / datamembers facilmente ..?

Ho circa 100 classi diverse, che mi serve per quei metodi.

Grazie

È stato utile?

Soluzione

If you really can't change the library, then I believe your only choice to expose the classes via WCF is to create wrapper objects for each method. I would look at writing a code generator for this task.

You can reflect over the set of types in the assembly that you want to expose to get the type metadata information that you need.

You can use something like t4 templates (part of VS 2008 and above) and the T4 Toolbox to create the code generator to write the code for you. Once the generator is done it should be easy to rerun again if your library ever changes. It's also easy to fix bugs updating the code generator and rerunning it.

The other option which I mention only for completeness but which has some thorny issues would be to disassemble and patch the code in question. You can use something like ildasm to dump the il of the assembly, add the necessary WCF attribution and then reassemble it with ilasm. However, the process can be error prone, any time the assembly changes you'll have to redo the process, there could be legal issues depending on who owns the IP of the assembly, and you'll have to re-sign the assembly, potentially with a different cryptographic cert if it needs to be a strong-named assembly.

* Edit *

Requested wrapper code sample:

public class ToWrap {
  public String Name { get; set; }
  public String Address { get; set; }
}

[DataContract]
public class Wrapper {
  private ToWrap _wrapped;

  // constructor for WCF marshalling
  public Wrapper() {
    _wrapped = new ToWrap();
  }

  public Wrapper(ToWrap wrapped) {
    _wrapped = wrapped;
  }

  [DataMember]
  public String Name {
    get { return _wrapped.Name; }
    set { _wrapped.Name = value; }
  }

  [DataMember]
  public String Address {
    get { return _wrapped.Address; }
    set { _wrapped.Address = value; }
  }
}

Altri suggerimenti

If those classes are marked as [Serializable] then you can still use them in a WCF ServiceContract, but if they aren't then there's no easy way to do it. You'd have no choice but to create a new set of classes that have the same properties, and then copy them property-by-property into the library's equivalents every time you wanted to invoke one of the library's functions.

Starting with .Net 3.5 SP1 you are no longer required to specify a DataContract. You can simply use the classes within your ServiceContract.

For a full explanation, check out this related question, which discusses when to use and when not to use a DataContract and the consequences.

I've never try it, but you could try adding the attributes using partial classes and partial methods.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top