Wcf datacontract for classes in a library which i cannot modify
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 开发者_C百科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
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; }
}
}
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.
精彩评论