Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / December 2004

Tip: Looking for answers? Try searching our database.

webmethod returning interface or  receiving an interface parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LS - 18 Nov 2004 17:28 GMT
Can a WebMethod return an Interface type?
Can we pass an interface parameter ?

Example :

public interface IEntity
{
    long Id { get; set; }
    string Name { get; set; }
}

[WebMethod]
public IEntity Fetch(long entityKey)
{
    ...
}

[WebMethod]
public bool Save(IEntity entity)
{
    ...
}

Thanks for your reply
Drew Marsh - 18 Nov 2004 18:16 GMT
> Can a WebMethod return an Interface type?
> Can we pass an interface parameter ?

The short answer is "no". You'll get an error just trying to generate any kind of service description along the lines of:

   Cannot serialize interface TestWebService.IFoo.

If you think about it, interfaces have no real correlation in XML schema. There's no way to represent the data. Base classes will work however.

HTH,
Drew
LS - 18 Nov 2004 19:45 GMT
Hello

"....interfaces have no real correlation in XML schema..."

In an interface you can declare public properties

If a class has public properties, the Serializer works ok. We were  
expecting the same, using the interface....

         Best regards

> > Can a WebMethod return an Interface type?
> > Can we pass an interface parameter ?
[quoted text clipped - 7 lines]
> HTH,
> Drew
Drew Marsh - 18 Nov 2004 23:14 GMT
> "....interfaces have no real correlation in XML schema..."
>
> In an interface you can declare public properties
>
> If a class has public properties, the Serializer works ok. We were
> expecting the same, using the interface....

Sure, but you're serializing the class, not the interface, in that case. All the web method knows about here is the interface, it has no clue what concrete types you're going to hand it and there's no way to decorate your interface with XmlIncludeAttribute since it's only applicable to classes (not interfaces). This is not even a web service limitation, it's an XML serializer limitation. The web service is just trying to use the XmlSerializer and ends up getting this exception:

[NotSupportedException: Cannot serialize interface TestWebService.IFoo.]
  System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, Boolean canBePrimitive, MemberInfo memberInfo)
  System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference)
  System.Xml.Serialization.TypeScope.GetTypeDesc(Type type)
  System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers)
  System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement)

Next, the XML serializer won't even look at attributes on interfaces. For example if you have something like:

public interface IFoo
{
[XmlAttribute("name")]
string Name { get; set; }
}

[XmlRoot("foo", Namespace="http://test.com/fooschema.xsd")]
[XmlType("FooA", Namespace="http://test.com/fooschema.xsd")]
public sealed class FooA : IFoo
{
 private string name;
   
 #region IFoo Members

 public string Name
 {
   get
   {
     return this.name;
   }

   set
   {
     this.name = value;
   }
 }

 #endregion
}

That class will serialize to:

<foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://test.com/fooschema.xsd">
 <Name>Test Foo!</Name>
</foo>

Notice the <Name> as opposed to what I decorated the IFoo as? If it obeyed IFoo's serialization attributes it would have looked like this (an attribute instead):

<foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://test.com/fooschema.xsd" name="Test Foo!"/>

HTH,
Drew
LS - 19 Nov 2004 12:49 GMT
Hello

That's the conclusion. It's a limitation in the XML Serializer.

1. It compiles OK.
2. When starting the WebService application, it throws the exception.

Why we need to specify parameters and return objects as Interfaces ?

We have a complex and huge application, where some Assemblies are
replaceable dynamically . So we need to take full advantage of object
oriented features. That's why we need the Interfaces.

When accessing Assembly A thru A's Interface (defined in Assembly IA), from
Assembly B, this strategy works ok, as in any other OO language.

When exposing A thru a Web Service, we have the problem. The XML Serializer
is not capable of doing with an Interface, the very same it does with a
class. There is no reason for that. The only limitation is that microsoft
didn't see this, and didn't implement it.

The direct consequence, is one big problem.

Solution 1:  Not take full advantage of OO features

Solution 2: Have a second definition of a property in the class implementation

public Interface IEntity
{
     IPhones phones {get; set; }
}

in the class that implements the interface

public class Entity: IEntity
{
    [XmlIgnore]
    IPhones IEntity.phones {get ..... ; set  ...... ; }   // Interface for
other Assemblies

    public Phones  phones {get ...... ; set ...... ; }   // For the
WebService,... not very nice...
}

What we hope:  VisualStudio.NET 2005 ===> please, look into this...

                            Best regards

                                   LS

> > "....interfaces have no real correlation in XML schema..."
> >
[quoted text clipped - 56 lines]
> HTH,
> Drew
Dan Rogers - 03 Dec 2004 20:42 GMT
You can get pretty close to this using schema inheritence (which maps to
class inheritence) instead of using interfaces.  The short of it is that
the web service standards do not support interfaces, so the extension of
this is that the CLR web service tooling and infrastructure also has no way
to support your requirement at the moment.

I hope this helps

Dan Rogers
Microsoft Corporation
--------------------
Thread-Topic: webmethod returning interface or  receiving an interface param
thread-index: AcTONicxHSMzv+4sQzeeqjry2/u8UA==
X-WBNR-Posting-Host: 81.193.248.227
From: "=?Utf-8?B?TFM=?=" <LS@discussions.microsoft.com>
References:  <DF8EB349-A12E-4036-9FF3-94A6DDD6C211@microsoft.com>
<#n8f$qZzEHA.3476@TK2MSFTNGP10.phx.gbl>
<83134CD5-00A1-4A8C-903A-B9ABAE2A68A3@microsoft.com>
<#YSofRczEHA.3028@TK2MSFTNGP10.phx.gbl>
Subject: Re: webmethod returning interface or  receiving an interface param
Date: Fri, 19 Nov 2004 04:49:05 -0800
Lines: 122
Message-ID: <77FF0055-2EB8-41FB-ABF9-D48A3C5C4C46@microsoft.com>
MIME-Version: 1.0
Content-Type: text/plain;
    charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.webservices
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA06.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08
phx.gbl!TK2MSFTNGXA03.phx.gbl
Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.webservices:7556
X-Tomcat-NG: microsoft.public.dotnet.framework.webservices

Hello

That's the conclusion. It's a limitation in the XML Serializer.

1. It compiles OK.
2. When starting the WebService application, it throws the exception.

Why we need to specify parameters and return objects as Interfaces ?

We have a complex and huge application, where some Assemblies are
replaceable dynamically . So we need to take full advantage of object
oriented features. That's why we need the Interfaces.

When accessing Assembly A thru A's Interface (defined in Assembly IA), from
Assembly B, this strategy works ok, as in any other OO language.

When exposing A thru a Web Service, we have the problem. The XML Serializer
is not capable of doing with an Interface, the very same it does with a
class. There is no reason for that. The only limitation is that microsoft
didn't see this, and didn't implement it.

The direct consequence, is one big problem.

Solution 1:  Not take full advantage of OO features

Solution 2: Have a second definition of a property in the class
implementation

public Interface IEntity
{
     IPhones phones {get; set; }
}

in the class that implements the interface

public class Entity: IEntity
{
    [XmlIgnore]
    IPhones IEntity.phones {get ..... ; set  ...... ; }   // Interface for
other Assemblies

    public Phones  phones {get ...... ; set ...... ; }   // For the
WebService,... not very nice...
}

What we hope:  VisualStudio.NET 2005 ===> please, look into this...

                            Best regards

                                   LS

"Drew Marsh" wrote:

> LS wrote:
>
[quoted text clipped - 6 lines]
>
> Sure, but you're serializing the class, not the interface, in that case. All the web method knows about here is the interface, it has no clue what
concrete types you're going to hand it and there's no way to decorate your
interface with XmlIncludeAttribute since it's only applicable to classes
(not interfaces). This is not even a web service limitation, it's an XML
serializer limitation. The web service is just trying to use the
XmlSerializer and ends up getting this exception:

> [NotSupportedException: Cannot serialize interface TestWebService.IFoo.]
>    System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, Boolean canBePrimitive, MemberInfo memberInfo)
>    System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference)
>    System.Xml.Serialization.TypeScope.GetTypeDesc(Type type)
>    
System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflec
tionMember xmlReflectionMember, String ns, XmlReflectionMember[]
xmlReflectionMembers)
>    
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlRefle
ctionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement)

> Next, the XML serializer won't even look at attributes on interfaces. For example if you have something like:
>
[quoted text clipped - 31 lines]
>
> <foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://test.com/fooschema.xsd">
>   <Name>Test Foo!</Name>
> </foo>
>
> Notice the <Name> as opposed to what I decorated the IFoo as? If it obeyed IFoo's serialization attributes it would have looked like this (an
attribute instead):

> <foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://test.com/fooschema.xsd" name="Test Foo!"/>

> HTH,
> Drew

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.