2
votes

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; }
}
2
can you show your service/data contract?deha
Issue seems to be somewhere else further down the pipeline. Show service web method and database writing code.Kon
constructors if IssueDataContract don't matchdeha
Think about what it would mean if .NET executed code out of sequence when there is a dependency on the sequence. After you think about it, you're realize your code is being executed in the correct order, so this is a different problem.John Saunders

2 Answers

3
votes

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.

1
votes

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.