473,729 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

webmethod returning interface or receiving an interface parameter

LS
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
Nov 21 '05 #1
5 10376
LS wrote:
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
Nov 21 '05 #2
LS
Hello

"....interf aces 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
"Drew Marsh" wrote:
LS wrote:
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

Nov 21 '05 #3
LS wrote:

"....interf aces 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 XmlIncludeAttri bute 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:

[NotSupportedExc eption: Cannot serialize interface TestWebService. IFoo.]
System.Xml.Seri alization.TypeS cope.ImportType Desc(Type type, Boolean canBePrimitive, MemberInfo memberInfo)
System.Xml.Seri alization.TypeS cope.GetTypeDes c(Type type, MemberInfo source, Boolean directReference )
System.Xml.Seri alization.TypeS cope.GetTypeDes c(Type type)
System.Xml.Seri alization.XmlRe flectionImporte r.ImportMemberM apping(XmlRefle ctionMember xmlReflectionMe mber, String ns, XmlReflectionMe mber[] xmlReflectionMe mbers)
System.Xml.Seri alization.XmlRe flectionImporte r.ImportMembers Mapping(XmlRefl ectionMember[] xmlReflectionMe mbers, String ns, Boolean hasWrapperEleme nt)

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

public interface IFoo
{
[XmlAttribute("n ame")]
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
Nov 21 '05 #4
LS
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.NE T 2005 ===> please, look into this...

Best regards

LS
"Drew Marsh" wrote:
LS wrote:

"....interf aces 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 XmlIncludeAttri bute 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:

[NotSupportedExc eption: Cannot serialize interface TestWebService. IFoo.]
System.Xml.Seri alization.TypeS cope.ImportType Desc(Type type, Boolean canBePrimitive, MemberInfo memberInfo)
System.Xml.Seri alization.TypeS cope.GetTypeDes c(Type type, MemberInfo source, Boolean directReference )
System.Xml.Seri alization.TypeS cope.GetTypeDes c(Type type)
System.Xml.Seri alization.XmlRe flectionImporte r.ImportMemberM apping(XmlRefle ctionMember xmlReflectionMe mber, String ns, XmlReflectionMe mber[] xmlReflectionMe mbers)
System.Xml.Seri alization.XmlRe flectionImporte r.ImportMembers Mapping(XmlRefl ectionMember[] xmlReflectionMe mbers, String ns, Boolean hasWrapperEleme nt)

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

public interface IFoo
{
[XmlAttribute("n ame")]
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

Nov 21 '05 #5
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+4 sQzeeqjry2/u8UA==
X-WBNR-Posting-Host: 81.193.248.227
From: "=?Utf-8?B?TFM=?=" <LS@discussions .microsoft.com>
References: <DF************ *************** *******@microso ft.com>
<#n************ **@TK2MSFTNGP10 .phx.gbl>
<83************ *************** *******@microso ft.com>
<#Y************ **@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: <77************ *************** *******@microso ft.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.publi c.dotnet.framew ork.webservices
NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.1.29
Path:
cpmsftngxa10.ph x.gbl!TK2MSFTNG XA06.phx.gbl!cp msftngxa06.phx. gbl!TK2MSFTNGP0 8
phx.gbl!TK2MSFT NGXA03.phx.gbl
Xref: cpmsftngxa10.ph x.gbl
microsoft.publi c.dotnet.framew ork.webservices :7556
X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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.NE T 2005 ===> please, look into this...

Best regards

LS
"Drew Marsh" wrote:
LS wrote:

"....interf aces 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 XmlIncludeAttri bute 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:
[NotSupportedExc eption: Cannot serialize interface TestWebService. IFoo.]
System.Xml.Seri alization.TypeS cope.ImportType Desc(Type type, Boolean canBePrimitive, MemberInfo memberInfo) System.Xml.Seri alization.TypeS cope.GetTypeDes c(Type type, MemberInfo source, Boolean directReference ) System.Xml.Seri alization.TypeS cope.GetTypeDes c(Type type)
System.Xml.Seri alization.XmlRe flectionImporte r.ImportMemberM apping(XmlRefle c
tionMember xmlReflectionMe mber, String ns, XmlReflectionMe mber[]
xmlReflectionMe mbers) System.Xml.Seri alization.XmlRe flectionImporte r.ImportMembers Mapping(XmlRefl e
ctionMember[] xmlReflectionMe mbers, String ns, Boolean hasWrapperEleme nt)
Next, the XML serializer won't even look at attributes on interfaces. For example if you have something like:
public interface IFoo
{
[XmlAttribute("n ame")]
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


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2663
by: sd | last post by:
Hello All, I aplogize if this has already been answered however I couldn't find anything related to this... I have bunch of webservices written in vb.net returning native data types, due to constraints of the client I need to return data that has been output escaped i.e. when a string response is returned back by the service it automatically converts & to amp; and the other reserved chracters, in this case I don't want the conversion to
2
5727
by: Alin Popovici | last post by:
Hi! I have this problem. I am sending as a parameter for a webmethod a string containing '\r\n' sequences. For some reason, when I debug my webmethod, the paramter is received with the carriage return characters ('\r') stripped. Can someone try this by creating a simple webmethod that takes a string as a parameter and returns the same string back. On the client, create a proxy, call the webmethod and pass: "bla\r\nbla". If you put a...
0
2200
by: George Durzi | last post by:
cross posted in datagrid group. Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be Completed By"> <itemtemplate> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FULL_NAME") %>'><asp:label> </itemtemplate> <edititemtemplate>
2
2845
by: Peter McEvoy | last post by:
Folks, I've been building a Webservice API for a contract that will be exposed to the internet at large. There are two endpoints, and each endpoint contains a number of webmethods. Every webmethod returns a returncode which indicates the level of success in performing the operation (as well as the actual results of the call in an "out" parameter) They have a requirement that when they want to perform maintenance on the backend system,...
1
2109
by: spicyz | last post by:
I'm receiving an exception when my webmethod exits after opening/closing an Access database. I can reproduce this easily enough by creating a new default ASP .NET Web Service in VS.NET 2003. Adding this code to the default HelloWorld() webmethod will cause the problem. String* sConnection = S"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=c:\\mydb.mdb";
2
6844
by: MrDotNet | last post by:
Hi I want pass NameValueCollection as parameter in webmethod. I try it but that give me error. Here is Error. You must implement the Add(System.String) method on System.Collections.Specialized.NameValueCollection because it inherits from ICollection. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
5
1636
by: John | last post by:
Hi Is it possible to pass a whole record to a webmethod? How? Thanks Regards
3
2969
by: Khurram | last post by:
Hi, Firstly, I will apologise now if I have posted in the wrong discussion group. Please let me know if I have for future reference. Below is the code to a WebMethod that is querying an Access 2003 database, saving the results as a dataset and returning the dataset. public DataSet findAAPAssessment(string assessmentID) {
1
1150
by: @rturo | last post by:
I have a webservice running and I have a method with a string parameter. The method is quering an xml file but i need to enter the exact letter case in the string to get the right reult otherwise is returning me and Exception. for example this xml... <location id="123">Finance</location> <location id="124">Operations</location> if the parameter i send is "finance" i'll get an exception. It will only let
0
9428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9293
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9221
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9162
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8165
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3246
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.