简体   繁体   中英

Returning an Object that contains a List member in WCF service

I have the follow classes in my Shared.DataContracts DLL

[DataContract]
public class TestClass
{
   [DataMember]
   public int Group { get; set; }

   [DataMember]
   public List<TestClassTwo> Member { get; set; }
}

[DataContract]
public class TestClassTwo
{
   [DataMember]
   public long MessageId { get; set; }

   [DataMember]
   public RequestOpType ResultType { get; set; }
}

[DataContract(Name = "RequestOperationType")]
public enum RequestOpType
{
   [EnumMember]
   Add,
   [EnumMember]
   Delete,
   [EnumMember]
   Update,
   [EnumMember]
   Retrieve,
   [EnumMember]
   Search
}

My service contract is as follows:

[ServiceContract]
public interface IConnectorService
{
    [OperationContract]
    void PustRequestToWorkBuffer(TestClass test); 
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ConnectorService : IConnectorService
{
    public void PustRequestToWorkBuffer(TestClass test)
    {
        return;
    }
}

When I try to add the following as a Service Reference to one of my projects I always get an error being thrown by WSDL. The project has a reference to Shared.DataContracts DLL. I therefore add the Service Reference by leaving the 'Re-use typres in referenced assemblies' check box ticked.

I get this error:

Error 73 Custom tool error: Failed to generate code for the service reference 'ServiceReference1'. Please check other error and warning messages for details. S:\\src\\test\\source\\test\\Connector\\Service References\\ServiceReference1\\Reference.svcmap 1 1 MetaConnector

The warning is:

Warning 68 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Referenced type 'Poc.Shared.DataContract.TestClass, Poc.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' with data contract name 'TestClass' in namespace ' http://schemas.datacontract.org/2004/07/Poc.Shared.DataContract ' cannot be used since it does not match imported DataContract. Need to exclude this type from referenced types. XPath to Error Source: //wsdl:definitions[@targetNamespace=' http://tempuri.org/ ']/wsdl:portType[@name='IConnectorService'] S:\\src\\source\\MetaConnector\\Service References\\ServiceReference1\\Reference.svcmap 1 1 MetaConnector

If I remove List<TestClassTwo> from the TestClass and replace it with just a TestClassTwo member it works fine. Looks like having a List is the cause of my problem.

I need to re-use the types in the shared assembly so un-checking this option is not applicable (that would remove the error though).

EDIT: I think the visual studio 'Add Service Reference' functionality is a bit flaky based on reading around. Since I controlled both the client and server I re-factored my code to share the contract and use a ChannelFactory instead. Although I did use the same contracts in a separate solution and everything seemed okay. I have a feeling the client and server solutions had some issues and they were looking at different versions of the Shared.DataContracts.DLL.

Try adding new DataContract like :

[DataContract]
public class ListTestClassTwo
{
   [DataMember]
   public List<TestClassTwo> Member { get; set; }

   public ListTestClassTwo()
   {
        Member = new List<TestClassTwo>();
   }
}

Then define it inside TestClass like:

[DataContract]
public class TestClass
{
   [DataMember]
   public int Group { get; set; }

   [DataMember]
   public ListTestClassTwo ListOfMembers { get; set; }
}

I think this should work.

OR Alternatively you can try KnownType for TestClassTwo over TestClass like:

[DataContract]
[KnownType(typeof(TestClassTwo))]
public class TestClass
{
   [DataMember]
   public int Group { get; set; }

   [DataMember]
   public List<TestClassTwo> Member { get; set; }
}

Update:

If you are reusing assembly make sure same version of assembly is referenced everywhere it is used. Each time I faced error having Cannot import wsdl:portType Detail , it's always something to do with mismatch of assembly versions which is reused.

Last time I faced similar issue it was that I added some attribute into some DataContract & it was reused but from where it was referenced had the old version not having new added attributes, that was the cause of issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM