WebForm中的WCF服务太快了?

时间:2011-11-01 20:44:45

标签: c# asp.net wcf web-services guid

我遇到一个奇怪的问题,当从WebForms事件处理程序内部调用时,我的WCF服务似乎太快被调用。

具体来说,传递给服务调用的Guid全部为零,好像尚未创建...

当我启动调试器并对 guid 变量进行监视时,我可以看到它确实被创建为有效的非零Guid。

以下是代码:

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

以下是合同:

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(来自IssueTrackerService.cs合约):

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

服务实施(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 个答案:

答案 0 :(得分:3)

我认为您的问题不在于您的“WCF服务过早被调用”。它可能正在某处重置或未正确序列化/反序列化。我会首先查看Guid上的RequestObject媒体资源。

此外,如果您正在使用http绑定,您还可以启动Fiddler并询问传出请求,以确保Guid值在您从客户端发送时是正确的。

答案 1 :(得分:1)

我不确定,但试着让它“更清晰”,因为理由似乎在其他地方。你正在搞乱界面属性(一般来说,这不是一个好主意,只有界面中的属性)和类属性,你“添加”一些变异器,然后标记为DataMembers ......我会拍摄那就是原因。

我会从改变

开始
  

公共类IssueDataContract:IIssue

  

公共类IssueDataContract

这个界面对我来说非常麻烦。