PositiveGuy
PositiveGuy

Reputation: 47743

Cannot convert source type to target type

In a code-behind for an .aspx in one of my test web projects, I'm trying to set this variable to the returned object. I've got the reference to the web service in my web project and that's fine.

I am making a call to one of our web service methods:

CompanyName.CC.DebitResponse debitRespnose = CCService.IssueDebit(...);

in the .asmx IssueDebit returns type CompanyName.CC.DebitResponse.

But I get this error:

"Cannot convert source type MyWebTestProject.CCService.DebitResponse to target type CompanyName.CC.DebitResponse"

I mean the variable I'm setting up and the return type defined in the method in my service are exactly the same. So why would it complain?

In my web service, here's IssueDebit:

public DebitResponse IssueDebit(...)
{
...
}

I know for sure that DebitResponse in the IssueDebit is of type CompanyName.CC.DebitResponse

UPDATED.

Ok, I'm very stuck. Here's the actual code (before I just changed the names for privacy purposes). Please do not re-paste any of this (or if you do drastically change the names as this is sensitive)

http://www.elbalazo.net/post/codebehind.txt and http://www.elbalazo.net/post/webservice.txt

this is the real code. Look at IssueDebit in the code behind.

The web service reference I have in my test web project is named LitleService.

When I look at that service in object explorer, I notice it has the type in it that's conflicting. So why would that type be in my service if I do not have that class even defined in my .asax?

http://www.elbalazo.net/post/webservice_objectexplorer.jpg

In another method I stubbed out in my codebehind, I'm getting Ambiguous reference on the param here:

    private void SetIssueDebitResults(ServiceAuthResponse response)
    {
        //not implemented
    }

Ambiguous Reference:
xxx.Litle.ServiceAuthResponse
WebServiceTesting.LitleService.ServiceAuthResponse

so why would I get ServiceAuthResponse objet in my Litle service like this when the actual ServiceAuthResponse class origin and definition is in xxx.Litle.ServiceAuthResponse...a completely different project? I've got that assembly referenced in both web projects (both the test project and my web project that contains the actual .asmx).

Upvotes: 2

Views: 8471

Answers (4)

Bobby
Bobby

Reputation: 11576

Seems like he's confusing different namespaces...if the variables are completely ident, then you can cast it.

CompanyName.CC.DebitResponse debitRespnose = (CompanyName.CC.DebitResponse)CCService.IssueDebit(...);

Upvotes: 0

Erik van Brakel
Erik van Brakel

Reputation: 23820

As far as I know, a .NET 2.0 webservice (asmx) automatically generates a mapping for the types needed to call a method (parameters and return types) in the WSDL.

When you add the webservice as a web reference to your other project (your website in this case), proxy classes are automatically generated. These are the ones in the MyWebTestProject.CCService namespace, in your case. I'm not sure what exactly is in your DebitResponse code, but your error message shows what's wrong:

"Cannot convert source type MyWebTestProject.CCService.DebitResponse to target type CompanyName.CC.DebitResponse"

The type returned by the proxy-class which calls the webservice for you is not of type CompanyName.CC.DebitResponse, which you use in your webservice. It's the class that's generated from the WSDL, more specifically: MyWebTestProject.CCService.DebitResponse. It will contain all the same data as the class you use in your webservice, but it's a distinctly different class. On the other hand, the methods that exist on the class in your webservice are not present.

So, simply put: You have to use:

MyWebTestProject.CCService.DebitResponse debitResponse = CCService.IssueDebit(...);

Upvotes: 0

AJ.
AJ.

Reputation: 16719

Further along Bobby's response, have you tried this?

CompanyName.CC.DebitResponse debitResponse = 
    CCService.IssueDebit(...) as CompanyName.CC.DebitResponse;

Upvotes: 0

Ian Kemp
Ian Kemp

Reputation: 29859

I'm betting that your webservice method is, intentionally or not, returning the wrong type.

Try putting this at the top of your webservice class:

using DebitResponse = CompanyName.CC.DebitResponse;

and then changing your method call to something like:

public DebitResponse IssueDebit(variable)
{
  // stuff...
}

If you post the code of the IssueDebit() method it would help.

Upvotes: 1

Related Questions