473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2146
"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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.