本文介绍了WCF服务在WebForm中调用太早了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题:从WebForms事件处理程序内部调用WCF服务似乎太早了.

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.

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

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

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

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.

代码如下:


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:

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

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合同):

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

服务实现(IssueTrackerService.cs):

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

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

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

推荐答案

我认为您的问题不在于您的"WCF服务被调用得太早".它可能在某处被重置或未正确序列化/反序列化.我先来看一下RequestObject上的Guid属性.

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.

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

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.

这篇关于WCF服务在WebForm中调用太早了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:27