Failed to create object of type MyObject for input MyObject

I have such a scenario where the webservice method that I consume in C # returns a Business object, when I call the webservice method with the following code, I get an exception "Cannot use an object of type ContactInfo to enter ContactInfo" in the .cs link of the web link class

The code:

ContactInfo contactInfo = new ContactInfo(); Contact contact = new Contact(); contactInfo = contact.Load(this.ContactID.Value); 

Any help would be greatly appreciated.

+6
c # web-services
source share
7 answers

This is because one of the ContactInfo objects is a web service proxy and is in a different namespace.

This is a known issue with asmx style web services. I used to implement an automatic shallow copy to get around it ( here as though, if I did it again, I would probably look at AutoMapper ).

For example, if you have an assembly with the following class:

 MyProject.ContactInfo 

and you return an instance of it from the web method:

 public class DoSomethingService : System.Web.Services.WebService { public MyProject.ContactInfo GetContactInfo(int id) { // Code here... } } 

Then, when you add the web link to your client project, you actually get this:

 MyClientProject.DoSomethingService.ContactInfo 

This means that if in your client application you call a web service to get ContactInfo , you have this situation:

 namespace MyClientProject { public class MyClientClass { public void AskWebServiceForContactInfo() { using (var service = new DoSomethingService()) { MyClientProject.DoSomethingService.ContactInfo contactInfo = service.GetContactInfo(1); // ERROR: You can't cast this: MyProject.ContactInfo localContactInfo = contactInfo; } } } } 

In this last line, I use the ShallowCopy class:

 namespace MyClientProject { public class MyClientClass { public void AskWebServiceForContactInfo() { using (var service = new DoSomethingService()) { MyClientProject.DoSomethingService.ContactInfo contactInfo = service.GetContactInfo(1); // We actually get a new object here, of the correct namespace MyProject.ContactInfo localContactInfo = ShallowCopy.Copy<MyClientProject.DoSomethingService.ContactInfo, MyProject.ContactInfo>(contactInfo); } } } } 

Note
This only works because the proxy class and the "real" class have exactly the same properties (one of them is generated by Visual Studio).

+10
source share

As mentioned by several other answers, precisely because .NET treats them as two different classes. I personally would recommend using something like AutoMapper . I use it and it seems pretty awesome. You can copy your objects in 1-2 lines of code.

 Mapper.CreateMap<SourceClass, DestinationClass>(); destinationInstance = Mapper.Map<SourceClass, DestinationClass>(sourceInstance); 
+1
source share

This is actually not a mistake. This is a problem with changing the version of your own project! Since your final launch does not use the original imported links when compiling!

For example, I did a chat server, a client. I used the package structure to transfer data on the client project. Then the same link to the server project is imported.

When casting Packet packet = (Packet)binaryFormatter.Deserialize(stream); I got the same error. Since the actual link to the server project is not a link to the client project! Because I rebuilt the client project many times!

When casting <new object>=(<new object>) <old object> always the new object must be newer or the same as the old object!

So, I made a separate project to create a DLL for the Packet class and imported the DLL file into both projects.

If I made any change to the Packet class, I need to import the link to the client and server again.

Then the casting will not give an exception!

+1
source share

How do you reference a class in a web service project, as well as a consumer project? If you just used the file link, this may explain the cause of the error. The serialialation method works for .NET (web services or, I suppose, I suppose), using reflection to load / unload object data. If the files are simply connected to each other, they are actually collected for different types in different assemblies, which explains why you have the same name, but cannot distinguish them. I recommend creating the "Core" library, which refers to both the web service and consumer projects, and contains the ContactInfo class, which you use everywhere.

0
source share

This is not a problem - this is a feature.

These are two independent classes. Compare the two and note that the proxy class does not have any of the constructors, methods, indexers, or other behavior from the original class. This is the same as using the ASMX service with a Java program.

0
source share

It looks like you have two different classes at both ends. Your application has a ContactInfo class, and your web service also has a ContactInfo class. Both are two completely different classes. One way is to use the WebService class on your side. If you use ContactInfo inside your web service, it will be serialized and will be available on the client side for use.

0
source share

You can also modify the References.cs file generated by Visual Studio when adding a web link. If you delete classes created using a proxy server and add the link (using the statements) to your personal classes, you can use them immediately, without a shallow copy / reflection or heavy display. (but you will have to reapply your modification if you upgrade the proxy server).

I also tried serializing the proxy object and deserializing it back in my DTO classes, but it was a pretty heavy resource, so I ended up modifying the layer generated using Cs links.

Hope this helps other people coming here :)

Kindly.

0
source share

All Articles