Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

c# - Unable to initialize member through Constructor in WCF

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

I am trying to assign values using : CibilEnquiryEnq.Tuef.Version = "12"; but it throws null reference error. I have already solved this error but through creating an object like : CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF(); and not through 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:(working)

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

I don't understand why I have to add CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF(); to make this work. I am already initializing tuef in constructor in my wcf.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The proxy objects generated by adding a service reference are not the same objects as you are defining in the service contract, they just happen to be created within the same namespace etc under the consuming clients service reference. Basically they are just DTOs that you use to consume the service.

If you want to have strong dependency between the objects then you can not use the service reference and you need to extract the contract to a separate assembly that you can reference.

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

2) CibilWcfService - This hosts the WCF service and refers the CibilWcfService.Contract assembly.

namespace CibilWcfService
{
    using CibilWcfService.Contract;

    public class CibilService : ICIBIL
    {
        // ... Interface implementation
    }
}

3) CibilClient - This is your consuming client application, it also refers the CibilWcfService.Contract assembly. You create the channel to the service like this, then the new CibilEnquiry() is using the same constructor as defined in your contract. You need to reference 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...