Unable to initialize element through constructor in WCF

I have a CibilResponse class that has class type properties (TUEF class).

I am trying to assign values ​​using: CibilEnquiryEnq.Tuef.Version = "12"; but it causes a null reference error. I already solved this error, but by creating an object like CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF(); , but not through the constructor.

ICIBIL.cs

 [ServiceContract] public interface ICIBIL { [OperationContract] string InsertCibil(CibilResponse cibilResponse); [OperationContract] string TestInsert(string testObj); [OperationContract] string GenerateEnquiry(CibilEnquiry testObj); } [DataContract] public class CibilEnquiry { [DataMember] public TUEF Tuef { get; set; } public CibilEnquiry() { this.Tuef = new TUEF(); } } [DataContract] public class TUEF { [DataMember] public string SegmentTag { get; set; } [DataMember] public string Version { get; set; } [DataMember] public string MemberReferenceNumber { get; set; } } 

Application: (not working)

 CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL(); CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry(); CibilEnquiryEnq.Tuef.Version = "1111"; // null reference error here and Tuef is null 

Application: (worker)

 CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL(); CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry(); CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF(); CibilEnquiryEnq.Tuef.Version = "1111";//works fine 

I do not understand why I need to add CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF(); to do this job. I am already initializing tuef in the constructor in my wcf.

I created a sample in a console application (wcf excluded) and it worked fine without Tuef = new TUEF(); Initialization in the constructor was enough.

+1
source share
1 answer

The proxy objects generated by adding a service reference are not the same objects that you define in the service contract, they are simply created in the same namespace, etc. according to customer service link. These are basically just the DTOs that you use to use the service.

If you want to have a strong dependency between objects, you cannot use the link to the service, and you need to extract the contract for a separate assembly that you can reference.

1) CibilWcfService.Contract - contains the ICIBIL interface + datacontract objects. You need to specify System.ServiceModel, System.ServiceModel.Web and System.Runtime.Serialization for the associated DataContract attributes.

2) CibilWcfService - it contains the WCF service and refers to the CibilWcfService.Contract assembly.

 namespace CibilWcfService { using CibilWcfService.Contract; public class CibilService : ICIBIL { // ... Interface implementation } } 

3) CibilClient is your consumer client application, it also refers to the CibilWcfService.Contract assembly. You create a channel for a service like this, then the new CibilEnquiry () uses the same constructor as in your contract. You need to refer to System.ServiceModel for ChannelFactory.

 using CibilWcfService.Contract; var cf = new ChannelFactory<ICIBIL>(); var channel = cf.CreateChannel(new EndpointAddress("http://127.0.01/CibilServiceUri")); if (channel != null) { var CibilEnquiryEnq = new CibilEnquiry(); CibilEnquiryEnq.Tuef.Version = "1111"; channel.GenerateEnquiry(CibilEnquiryEnq); } 
+3
source

All Articles