3

hi i have a class library that which performs methods and has a lot of different classes which it uses as parameters for the methods calls... i am creating a wcf wrapper for this class library. but I do not have permission to change the class library.

now my question is how can i expose these classes as data contracts/datamembers easily .. ?

I have about 100 different classes which i need for those methods.

Thanks

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Prakash
  • 471
  • 1
  • 8
  • 19

4 Answers4

2

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; }
  }
}
Peter Oehlert
  • 16,368
  • 6
  • 44
  • 48
  • Hi Thanks for your reply .. I think creating a wrapper around those classes is the best option for me .. Could you please give an example of how to create a wrapper for a small class of two properties.. Only code.. i will look into the t4 templates later .. Thanks – Prakash Mar 17 '11 at 21:51
  • @Prakash - Can you clarify which code you would like. Keep in mind that what I propose is you write code that will write more code. The wrapper code will be the output of your code generator and should be fairly simple. The reflection code to get the type info and all of the necessary metadata to create a generator to write that wrapper code for a given set of Type inputs will be more involved. I'd schedule myself probably 3 business days to write it. – Peter Oehlert Mar 17 '11 at 21:58
  • I would just like a small example of how a wrapper class would be .. Lets say i have a class which has two public properties.. perhaps public class customer{ public string name; public string address;} how would a wrapper for that be.. ? – Prakash Mar 17 '11 at 22:02
2

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.

Chris Wenham
  • 23,679
  • 13
  • 59
  • 69
1

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.

Community
  • 1
  • 1
Jacob
  • 453
  • 5
  • 17
-2

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

Jonathan
  • 11,809
  • 5
  • 57
  • 91
  • 2
    -1 This would never work. Partial classes are implemented by the compiler, not the runtime. They are resolved at compile time. This would involve modifying the library. – Peter Oehlert Mar 17 '11 at 19:35