Reputation: 11
Let me explain my question : i have a shared type let's call it "SharedType". Then I have multiple wcf services under multiple namespaces using that type example : Service1.SharedType , Service2.SharedType ... Can i serialize all of these under the same xml namespace and name?? each of my clients have a type of name "SharedType" and namespace "www.datacontract.org".I noticed that the first service to run encounters no problem i can use SharedType on the respectif client.When i start the second, i cannont use that type on it's client , it tells me that the type is not expected and that i should use a resolver... Is there a way to force the acceptance, i need them this way on purpose. And yeah i use wcf class library not wcf applications.
Thank you Smile | :)
Upvotes: 1
Views: 380
Reputation: 2613
The way to fix this problem is to put the shared types into a dll. Then reference that DLL in the server project and then client project. As long as you use the same DLL you should be fine. (Assuming you're using data contract serializer here.)
Here is an example of how you'd do this sort of thing. Notice how Person is in the DLL. https://github.com/Aelphaeis/MyWcfPipeExample
If you happen to be using a duplex then you can look at this example instead. Same thing without the complex type. https://github.com/Aelphaeis/MyWcfDuplexPipeExample
Another way to fix this problem is to actually take the object and serialize it into a string and remove the name spaces. Pass it to the service and deserialize it manually. This is rather clumbersome and I don't recommend this unless you cannot use data contract serializer and you're using older web services (.asmx from 3.5); however, if you are dead set on this crazy idea you can do the following :
public class NoNameSpaceXmlWriter : XmlTextWriter
{
public NoNameSpaceXmlWriter(TextWriter output) : base(output) { Formatting = Formatting.Indented; }
public override void WriteStartDocument() { }
public override void WriteStartElement(string prefix, string localName, string ns) { base.WriteStartElement("", localName, ""); }
}
and the usage would be something like this:
new XmlSerializer(ret.GetType()).Serialize(new NoNameSpaceXmlWriter(ms), ret);
where ret is some random object.
Upvotes: 1