简体   繁体   中英

Exposing existing class library to WCF service

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? eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM