0

I created 3 projects: class library (referenced by two following), web service and client application. When in client application I try to get an object of "shared type" from web service I get an error Cannot implicitly convert type 'ClientProject.ServiceProject.AB' to 'ClassLibrary.AB'. I thought that this is problem with how I used DataContract but I tried several approaches and it still gives the same result. Could you please give me some directions what do I do wrong?

Class library AB class:

using System;
using System.Runtime.Serialization;

namespace ClassLibrary
{
    [Serializable]
    [DataContract(Name = "AB", Namespace = "ClassLibrary")]
    public class AB
    {
        [DataMember(Name = "a")]
        private int a;
        [DataMember(Name = "b")]
        private byte[] b;
        <some_more_methods_here>
    }
}

Web service method:

using ClassLibrary;
[...]
[WebMethod]
public AB calledMethod(byte[] x)
{
    int a = [...]
    byte[] b = [...]
    AB ab = new AB(a, b);    
    return ab;
}

Failing client code:

using ClassLibrary;
[...]
byte[] x = [...]
var serviceProjectSoapClient = new ServiceProject.ServiceProjectSoapClient();
AB ab = new AB();
ab = serviceProjectSoapClient.calledMethod(x);

Edit:

As there were some concerns about whether I used shared project or Class library here's some clarification.

I tried the following: 1) created "Class Library" project; 2) built it; 3) referenced it's *.dll from both server and client application (both point to exactly the same *.dll file in the same location and when I inspect the type in both project both show me exactly the same from metadata).

TajniakOsz
  • 83
  • 1
  • 1
  • 9
  • 1
    Are you sure you wanted a shared library and not a class library? – ProgrammingLlama Dec 03 '19 at 04:57
  • @John_ReinstateMonica to be honest I don't know yet what the difference really is. I needed to share several classes so I created separate solution with these, built it and added a reference to the other projects. – TajniakOsz Dec 03 '19 at 05:02
  • [Class library vs shared project](https://stackoverflow.com/questions/30634753/what-is-the-difference-between-a-shared-project-and-a-class-library-in-visual-st) – ProgrammingLlama Dec 03 '19 at 05:22
  • As far as I understand it doesn't really have anything to do with my issue, or am I wrong? – TajniakOsz Dec 03 '19 at 05:27
  • Part of what makes a type a type is the assembly it's compiled in. If you are actually using a shared project instead of a class library (it's currently unclear if that's the case) then you are compiling that class into AssemblyA and AssemblyB (the two assemblies that reference the shared project). Therefore you will effectively have AssemblyA.SharedLibrary.AB and AssemblyB.SharedLibrary.AB, which are different types according to .NET. – ProgrammingLlama Dec 03 '19 at 05:30
  • I tried the following: 1) created "Class Library" project; 2) built it; 3) referenced it's *.dll from both server and client application. As far as I understand as both reference exactly the same *.dll in the same location I use Class Library. – TajniakOsz Dec 03 '19 at 05:50
  • OK, so you are using a class library? Hmm. That's weird then. – ProgrammingLlama Dec 03 '19 at 05:52

1 Answers1

0

I found why it behaves such way: I added Service Reference to the Client app project to communicate with the Web service. Such reference works in this case exactly like any other *.dll reference. The problem is despite Service method returns ClassLibrary.AB object, compiler does not take namespace into account and expects AB object, that because of the reference to the Web service is expected to be ServiceProject.AB and finally using default namespace in CientProject - ClientProject.ServiceProject.AB.

TajniakOsz
  • 83
  • 1
  • 1
  • 9