简体   繁体   中英

C# Web Service not picking up custom class with IIS

Currently using Visual Studio 2015 to build a web service application using .NET 4.0.

My interface class has 1 method that I want exposed on the service:

public interface IEvaluateGroupService{

    [OperationContract]
    EvaluateGroupResponse EvalGroup(EvaluateGroupRequest _request)

}

EvaluateGroupResponse and EvaluatGroupRequest are custom classes used by the web service.

My issue is that when I build the service and deploy it to IIS, it recognizes the existence of the Request/Response classes when I reference it, but it's missing the fields in the classes.

Ie if I reference my webservice in C#:

EvaluateGroupService.EvaluateGroupRequest _request = new EvaluateGroupService.EvaluateGroupRequest();

This doesn't throw an error. I can't however access any of the fields in my class. Additionally, I defined my request class's constructor to take 1 argument. If I try to call that constructor, I get an error stating that my request doesn't have a constructor that takes 1 argument.

Below are my Request/Response classes:

Request:

namespace EvaluateGroupService
{
[DataContract]
public class EvaluateGroupRequest
{
    private int accountKey;
    private DateTime? startDate = null;
    private DateTime? endDate = null;


    public EvaluateGroupRequest(int initAcctKey)
    {
        AccountKey = initAcctKey;

    }
    public DateTime? EndDate
    {
        get
        {
            return endDate;
        }

        set
        {
            endDate = value;
        }
    }
    public DateTime? StartDate
    {
        get
        {
            return startDate;
        }

        set
        {
            startDate = value;
        }
    }
    public int AccountKey
    {
        get
        {
            return accountKey;
        }

        set
        {
            accountKey = value;
        }
    }
}

Response:

namespace EvaluateGroupService
{
[DataContract]
public class EvaluateGroupResponse

{
    private bool success;
    public bool Success
    {
        get
        {
            return success;
        }

        set
        {
            success = value;
        }
    }
}
}

I think you are missing DataMember attribute.....

 [DataMember]
 public DateTime? EndDate
    {
        get
        {
            return endDate;
        }

        set
        {
            endDate = value;
        }
    }

You need to decorate the fields in your requests/responses with the [DataMember] attribute.

You used to have to add the DataContract attribute to the class, but that is now added by default. However, WCF doesn't assume the fields are accessible.

[DataContract]
public class MyResponse
{
    [DataMember]
    public string MyString { get; set; }
}

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