1

I have a problem with a simple C# web service from a sample. This is my server side code:

public delegate string LengthyProcedureAsyncStub(int milliseconds);

    public class MyState
    {
        public string text;
        public object previousState;
        public LengthyProcedureAsyncStub asyncStub;
    }

    [WebMethod]
    public IAsyncResult BeginLengthyProcedure(int milliseconds, AsyncCallback cb, object s)
    {
        LengthyProcedureAsyncStub stub = new LengthyProcedureAsyncStub(LengthyProcedure);
        MyState ms = new MyState();
        ms.previousState = s;
        ms.asyncStub = stub;
        return stub.BeginInvoke(milliseconds, cb, ms);
    }

    public string LengthyProcedure(int milliseconds)
    {
        System.Threading.Thread.Sleep(milliseconds);
        return "Success";
    }

    [WebMethod]
    public string EndLengthyProcedure(IAsyncResult call)
    {
        MyState ms = (MyState)call.AsyncState;
        string result = ms.asyncStub.EndInvoke(call);
        return result;//ms.text;
    }

I consume the service from a client like so:

private void button1_Click(object sender, EventArgs e)
    {

        Waiter(5000);

    }

    private void Waiter(int milliseconds)
    {
        asyncProcessor.Service1SoapClient sendReference;
        sendReference = new asyncProcessor.Service1SoapClient();
        sendReference.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
        sendReference.BeginLengthyProcedure(milliseconds, ServiceCallBack, null);

    }

    private void ServiceCallBack(IAsyncResult result)
    {
        string strResult = result.ToString();
    }

The problem is that the client variable strResult should have "Sucess" as it's value, instead it has this: "System.ServiceModel.Channels.ServiceChannel+SendAsyncResult". What am I doing wrong or overlooking?

Thanks in advance fro your time :)

4

1 に答える 1

0

sendReferenceの代わりにパスする必要がありnullます。

private void Waiter(int milliseconds)
{
    asyncProcessor.Service1SoapClient sendReference;
    sendReference = new asyncProcessor.Service1SoapClient();
    sendReference.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
    sendReference.BeginLengthyProcedure(milliseconds, ServiceCallBack, sendReference);

}

private void ServiceCallBack(IAsyncResult result)
{
    asyncProcessor.Service1SoapClient sendReference = result.AsyncState as asyncProcessor.Service1SoapClient;
    string strResult = sendReference.EndLengthyProcedure(result);
}
于 2012-10-03T23:30:47.197 に答える