472,993 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

.NET webservice returns a class that can not be cast

This is only a model program, because it would take too long to
demonstrate the real solution. Anyway,
I have 3 projects - ClassLibrary1, ConsoleApplication1 and WebSite1
(which hosts webservice).
ClassLibrary1 looks like this:
------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary1
{
public interface IClass1
{
string Message
{
get;
set;
}
}
}
--------------------------------------------
WebService code file looks like this:
--------------------------------------------
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
public class Class1:ClassLibrary1.IClass1
{
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public Class1 HelloWorld() {
Class1 cl = new Class1();
cl.Message = "Hello World";
return cl;
}

}
-------------------------------------------------------------
And the console application looks like this:
-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
localhost.Service sr = new
ConsoleApplication1.localhost.Service();
ClassLibrary1.IClass1 cl =
(ClassLibrary1.IClass1)sr.HelloWorld();
Console.WriteLine(cl.Message);
Console.ReadKey();
}
}
}
-----------------------------------------------------------
(localhost is the name of my web-reference to webservice)
both the webservice and consoleApplication holds references to
ClassLibrary1.

The problem is that when I try to run the solution, I get
InvalidCastException in my ConsoleApplication1 on this line:
ClassLibrary1.IClass1 cl = (ClassLibrary1.IClass1)sr.HelloWorld();
I see no reasons why upcasting should fail here, maybe anyone of you
has any ideas about my problem?

Oct 19 '06 #1
3 2134
"LT_Hassan" <tu****@gmail.comwrote in news:1161238967.029467.212270
@m7g2000cwm.googlegroups.com:
The problem is that when I try to run the solution, I get
InvalidCastException in my ConsoleApplication1 on this line:
ClassLibrary1.IClass1 cl = (ClassLibrary1.IClass1)sr.HelloWorld();
I see no reasons why upcasting should fail here, maybe anyone of you
has any ideas about my problem?
Ya unfortunately you can't just cast a web service class back to the
interface class ... There are work arounds, but it's generally not
recommend to pass typed data via web services.

If you need to pass typed data - use (IIS Hosted) Remoting instead.
Oct 19 '06 #2
thank you for your reply.
Could you give me any information about those work-arounds?

On Oct 19, 11:22 am, Spam Catcher <spamhoney...@rogers.comwrote:
"LT_Hassan" <tur...@gmail.comwrote in news:1161238967.029467.212270
@m7g2000cwm.googlegroups.com:
The problem is that when I try to run the solution, I get
InvalidCastException in my ConsoleApplication1 on this line:
ClassLibrary1.IClass1 cl = (ClassLibrary1.IClass1)sr.HelloWorld();
I see no reasons why upcasting should fail here, maybe anyone of you
has any ideas about my problem?Ya unfortunately you can't just cast a web service class back to the
interface class ... There are work arounds, but it's generally not
recommend to pass typed data via web services.

If you need to pass typed data - use (IIS Hosted) Remoting instead.
Oct 19 '06 #3
"LT_Hassan" <tu****@gmail.comwrote in
news:11**********************@f16g2000cwb.googlegr oups.com:
thank you for your reply.
Could you give me any information about those work-arounds?

You need to implement IXMLSerializble:

http://weblogs.asp.net/cweyer/archiv...02/205798.aspx

Also, when you generate your web service reference, I believe you have to
do it manually using the WSDL.exe tool (not via Visual Studios).

As you see - using web services to return custom data types adds a lot of
programming overhead... so it's better to use Remoting.

In anycase, the moment you start using custom data types - your web service
is no longer "generic" thus not compatible with other systems anyways
(other systems will need to implement custom parsing of your data types).
Oct 19 '06 #4

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

Similar topics

1
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been...
4
by: Adrian Meyer | last post by:
Hi, On the server I code the following web service: ))] public XmlDataDocument GetTypedXmlDataDocument() { sqlDataAdapter1.Fill(typedDataSet1); XmlDataDocument dataDoc
5
by: mtv | last post by:
Hi all, I have the following code: ================================ Webservice side: public class MyWS: WebService { private myLib.DataObject curDataObject;
4
by: Martin Ehrlich | last post by:
Hello NG. I've got a little problem with sharing types between webservices and clients. I've created a business class with public fields within a shared assembly like: public class Item {
9
by: Nick Gilbert | last post by:
Hi, I'm trying to create a system whereby my desktop application submits it's order to an online server using a webservice (pretty standard!). So, I added a new project to my solution to...
7
by: Nalaka | last post by:
Hi, I created a sinple web service that returns a dataSet. Then I created a client program that uses this web service (that returns the Dataset). My question is, how did the client figure...
1
by: Mike9900 | last post by:
I want to use an interface in both the webservice and its client. For example, the web service implements an interface and then the client cast that webservice to the inteface. It seems impossible,...
4
by: dan655t | last post by:
Hello, I've come here to get some advise from the experts. Currently I am tasked with modifying a web service that currently supports one product to support multiple products. It needs to be...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.