views:

90

answers:

1

Hi guys, I have an existing class library with all the classes to communicate with my server. I've created a WCF service that will be hosted on my existing server as part of different application domain. Since I already have the classes I thought it can be exposed on my WCF service to lessen my development time.

I successfully hosted my WCF service and is running on my dev pc. The problem is on the client side that adds my web service. They can only use the base classes of my library.

How can I make all of my classes aside from the base class of my library to be available on my web service? e.g. helper classes, child classes that inherited from my base class and other classes that is used on my generic collection.

All of my classes are dressed with Serializable and DataContract attributes.

By the way my class library was created 3 years ago I just patched some new attributes to make it available on my web service.

A: 

Helper classes that are not part of the hierarchy of the classes that are part of the operation contracts will never be exposed. As for child classes you could use the <knownType> section in web.config to instruct the serializer what are the possible child classes for a given base type:

<system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="SomeNs.MyBaseType, SomeNs">
          <knownType type="SomeNs.MyChildType1, SomeNs"/>
          <knownType type="SomeNs.MyChildType2, SomeNs"/>
        </add>
        <add type="SomeNs.MyBaseType2, SomeNs">
          <knownType type="SomeNs.MyChildType3, SomeNs"/>
        </add>
      </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

WCF will look at the types that are part of the operation contracts (the methods marked with [OperationContract]) and it will automatically expose those classes in the metadata (WSDL) so that clients will see them. Possible child classes need to be specified explicitly.

Also you might find this blog post helpful.

Darin Dimitrov
I'm self-hosting my wcf service. What part of App.Config should I put system.runtime.serialization?
powerbox
Directly at the root: `<configuration><system.runtime.serialization>...</configuration>`
Darin Dimitrov
I'm going to test your suggestions. Thanks! If this works, I need to add a whole lot of classes on my App.Config.
powerbox
Thanks Darin! It worked perfectly, though the client side can't use my helper classes they can use now my child classes.
powerbox
I would recommend you externalize your helper classes in a separate assembly. This will allow you to reuse it in your client application (assuming the client is written in .NET of course).
Darin Dimitrov
Darin got some problems again. My child classes only exposed 0 arguments on its constructor and it initializes its base class using constants but it seems that in WCF client class creation, this does not apply, my member variables are all set to null. This is really the behavior of class instantiation if it is exposed on WCF?
powerbox
The entities that are transported over the wire must have a default constructor because it is the only one that will be used by the serializer. They should be plain POCO objects. As you can see it's not that easy to expose an existing class library :-)
Darin Dimitrov
Thanks! @_@ I thought I'm going to save more time but turn out it is not. Anyway thanks for the answers , it helps me a lot!.
powerbox