Well I have been working on the project with WCF web service with some polymorphism to make it extensible.
So here it is the situation:
Projects
- Library project - contains few interfaces and the base classes for the entities that are exposed so let's say that there we have
[IDataContract]
public abstract class Entity
- WCF web service project - contains interface for the web service and the actual web service
[IServiceContract]
public interface IMyService
{
public Entity GetEntity(Guid ID);
}
The service itself loads from the database what type of Entity to instantiate and does that using Reflection
- Email project - this is the first project that references the Library project and inherits from the Entity class.
public EmailEntity:Entity
so everything worked just fine when using the development server, but the troubles began when I decided to host the web service in IIS 6.
I have made the svc file, the correct web.config, and I got WSDL, created client application, and when I have tried to call GetEntity ops I got exception, kind a strange one.
I have used the Object Browser in VS to check what is wrong, and here it was - there was no EmailEntity type. Well this was normal, the web service did not know anything about that type at the compilation. I have started to dig and here is what I have found.
If you use KnownType attribute in the code then you can inform the web service that the EmailEntity is avaliable, but in the code....
So looked a bit more and found
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add ......
<knownType
type="Email.EmailEntity,Email,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
This was the way to expose the type just in the configuration.
Labels: knownTypes, polymorphism, reflection, WCF