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