WCF Service called too soon in WebForm?

自古美人都是妖i 提交于 2019-12-25 03:32:31

问题


I'm having an odd issue where it seems my WCF service is being called too soon when called from inside a WebForms event handler.

Specifically, the Guid being passed to the service call is all zeros, as if it hasn't been created yet...

When I fire up the debugger and put a watch on the guid variable, I can see that it indeed is being created as a valid, non-zero Guid.

Here's the code:

protected void button_click(object sender, EventArgs e)
{
    var title = titleTextbox.Text;
    var guid = Guid.NewGuid();

    var CreateIssueResponse = proxy.CreateIssue(new CreateIssueRequest
    {                                                              
        User = user,                                       
        IssueDataContract = new IssueDataContract(title, guid)
    });

    dataBind();
}

Here are the contracts:

CreateIssueRequest.cs:

[DataContract(Namespace = "my-service-namespace")]
public class CreateIssueRequest : RequestBase
{

    public CreateIssueRequest() { }

    public CreateIssueRequest(UserDataContract user, IssueDataContract issue)
    {
        UserDataContract = user;
        IssueDataContract = issue;
    }

    [DataMember]
    public UserDataContract UserDataContract;

    [DataMember]
    public IssueDataContract IssueDataContract;
}

IssueDataContract.cs

[DataContract]
public class IssueDataContract : IIssue
{
    internal IssueDataContract() { }

    public IssueDataContract(string title, Guid guid)
    {
        Title = title;
        Guid  = guid;
    }

    [DataMember]
    public int? ID { get; internal set; }

    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public DateTime? DateCreated { get; internal set; }

    [DataMember]
    public string SupportAgentName { get; internal set; }

    [DataMember]
    public string Status { get; internal set; }

    [DataMember]
    public Guid Guid { get; set; }
}

CreateIssue (from IssueTrackerService.cs contract):

[ServiceContract(Name = "IIssueTrackerService", Namespace = "my-service-namespace")]
public interface IIssueTrackerService
{        
    [OperationContract]
    [FaultContract(typeof(FaultBase))]
    [FaultContract(typeof(ArgumentException))]
    [FaultContract(typeof(ArgumentNullException))]
    CreateIssueResponse CreateIssue(CreateIssueRequest request);
}

Service Implementation (IssueTrackerService.cs):

public class IssueTrackerService : IIssueTrackerService
{
    readonly IUserIssueRepository userIssueRepository;

    public IssueTrackerService(IUserIssueRepository userIssueRepository)
    {
        this.userIssueRepository = userIssueRepository;
    }

    public CreateIssueResponse CreateIssue(CreateIssueRequest request)
    {
        // Extract the user from the request, and validate
        var user  = request.UserDataContract;
        userValidator.Validate(user, true);

        // Extract the issue from the request, and validate
        var issue = request.IssueDataContract;
        issueValidator.Validate(issue, true);

        // If the user doesn't exist, add them via the repo
        if (userIssueRepository.GetUser(user.ID) == null)
            userIssueRepository.AddUser(user.ToEntity());

        // Add the issue via the repo, record the new issue id
        var issueId = userIssueRepository.AddIssue(user.ToEntity(), issue.ToEntity());

        // Get the issue with it's updated fields from the db
        var issueUpdate = userIssueRepository.GetIssue(issueId);

        // Prepare and return the response
        var response = new CreateIssueResponse { IssueDataContract = issueUpdate.ToDataContract() };
        return response;
    }
}

SqlUserIssueRepository.cs

public class SqlUserIssueRepository : IUserIssueRepository
{
    readonly UserIssueEntities db;

    public SqlUserIssueRepository()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        db = new UserIssueEntities(connectionString);
    }

    // User and Issue are EF complex types that implement IUser & IIssue respectively.
    // The IIssue interface defines a property for Guid
    public int AddIssue(User user, Issue issue)
    {
        db.CreateUser(user.ID, user.Username, user.FirstName, user.LastName, user.Email, user.Phone);
        return user.ID;
    }
}

IIssue.cs

public interface IIssue
{
    int?      ID               { get; }
    string    Title            { get; set; }
    DateTime? DateCreated      { get; }
    string    SupportAgentName { get; }
    string    Status           { get; }
    Guid      Guid             { get; set; }
}

回答1:


I don't think your problem is that your "WCF service is being called too soon". It's probably being reset somewhere or not serializing/deserializing correctly. I would look at your Guid property on your RequestObject first.

Also, if you're using a http binding, you could also fire up Fiddler and interrogate the outgoing request to ensure the Guid value is correct as it is being sent from your client.




回答2:


I am not sure, but try to make it "clearer" as reason seems to be somewhere else. You are messing interface properties (which is not a good idea in general to have only properties in interface) with class properties, you "added" some mutators and then marked as DataMembers... I would shoot that there's the reason.

I would start from changing

public class IssueDataContract : IIssue

to

public class IssueDataContract

that interface is very messing things up to me.



来源:https://stackoverflow.com/questions/7972658/wcf-service-called-too-soon-in-webform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!