1
votes

I have created a WCF service which return type of JsonOutput<List<InstallerRequestModel>>. My JsonOutput is a generic class which accepts any type

   public class JsonOutput<T> where T : new()
    {
        public string Status { get; set; }
        public T Result { get; set; }
    }

Service Endpoint

  [OperationContract]
        JsonOutput<List<InstallerRequestModel>> GetInstallerRequests(string requestType);

Installer Request Model

 public class InstallerRequestModel
    {
       [Key]
       public int Id { get; set; }
       public string Project { get; set; }
       public string ProductVersion { get; set; }
       public string ProductType { get; set; }
       public string PackageType { get; set; }
       public string NeedOfInstaller { get; set; }
       public string Label { get; set; }
       public DateTime? DueDate { get; set; }
       public string ResourceName { get; set; }
       public string RequestedBy { get; set; }
       public string BuildEngineer { get; set; }
       public string BuildPath { get; set; }
       public DateTime StartDate { get; set; }
       public DateTime EndDate { get; set; }
       public string BuildStatus { get; set; }
       public string ApprovedBy { get; set; }
       public string CreatedBy { get; set; }
       public string ModifiedBy { get; set; }

    }

the problem after creating by proxy JsonOutput<List<InstallerRequestModel>> change to JsonOutputOfArrayOfInstallerRequestModelohl1y18V.

 public JsonOutputOfArrayOfInstallerRequestModelohl1y18V GetInstallerRequests(string requestType)
        {
            using (var buildPortalServiceClient = new BuildPortalServiceClient())
            {
                return buildPortalServiceClient.GetInstallerRequests(requestType);
            }
        }

I got the result exactly i am expected.But How can i keep the same class name?

1
Use System.Collections.Generic.List<T> for collections and reuse types from referenced assemblies. - CodeCaster
i already select System.Collections.Generic.List when creating proxy.but still the problem exists. - Jameel Moideen
I have no option of type List<T> in collection type dropdown.only have the type List - Jameel Moideen
i think the problem is because of JsonOutput<T> class - Jameel Moideen
WCF exchange messages in XML format, and to serialize types to XMK it has to be of known types, and that does not work with generic types.. - Nean Der Thal

1 Answers

1
votes

The way to do it is to share your service contract and data contract classes with the client project.

I created a new project from the "WCF Service Library" template in Visual Studio. I added your JsonOutput<T> and InstallerRequestModel classes to it, and added your operation contract to the IService1 service contract. I implemented a dummy GetInstallerRequests operation in the Service1 class so that it would still build.

I took a web application project I had sitting around and added the WCF Service Library to it as a reference (note: not a service reference). This gave that project access to the actual types used in the actual service contract. After getting the WCF Service Library project hosted and starting the host, I was able to add a service reference to the web application. I used the "Advanced" button and made sure that "Reuse types in referenced assemblies" was checked. I confirmed that the referenced library was one of those assemblies. I then clicked OK twice, and Visual Studio created the proxy classes.

Here is one of the methods that it generated. You can see that it references the correct type:

public WcfServiceLibrary1.JsonOutput<System.Collections.Generic.List<WcfServiceLibrary1.InstallerRequestModel>>
    GetInstallerRequests(string requestType) {
        return base.Channel.GetInstallerRequests(requestType);
}