无法通过构造函数,WCF初始化成员初始化、函数、成员、WCF

2023-09-04 01:20:36 作者:小三终会输给了小四

我有一个CibilResponse类,具有类类型(TUEF类)。

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

我试图来分配使用的值: CibilEnquiryEnq.Tuef.Version =12; ,但它抛出空引用错误。我已经解决了这个错误,但通过创建一个对象,如: CibilEnquiryEnq.Tuef =新CibilWcfService.TUEF(); ,而不是通过构造

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; }
   }

应用程序:(不工作)

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

应用程序:(工作)

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

我不明白为什么我要补充 CibilEnquiryEnq.Tuef =新CibilWcfService.TUEF(); 来完成这项工作。我已经初始化tuef在构造函数中我的WCF。

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.

我创建了一个控制台应用程序(不包括WCF)的样本,它能正常工作,而无需 Tuef =新TUEF(); ,初始化的构造是足够

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

推荐答案

通过添加服务引用生成的代理对象不是因为你是定义在服务合同相同的对象,他们只是碰巧在同一个命名空间中创建在消费客户服务引用等。基本上,他们只是DTO的,你用它来消费服务。

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 - 包含ICIBIL接口+ datacontract对象。您需要引用System.ServiceModel,System.ServiceModel.Web和System.Runtime.Serialization为DataContract相关的属性。

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 - 这承载WCF服务,是指在CibilWcfService.Contract组装

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)CibilCli​​ent - 这是你的消费客户端应用程序,它也指了CibilWcfService.Contract组装。创建信道到这样的服务,那么新CibilEnquiry()是使用相同的构造作为合同中所定义。您需要引用System.ServiceModel进行的ChannelFactory。

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);
}