1

我有一个 wcf 服务,我正在调用这样的方法:

public static void f5()
    {
        var client = new WebClient();
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        var data = File.ReadAllText("request.xml");
        client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
        client.Headers.Add("SOAPAction", "some string");            
        client.UploadStringAsync(new Uri("http://differentdomain/wcf/Service.svc"), data);
    }

    public static void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        Console.WriteLine(e.ToString());
        int cow = 0;
        cow++;
    }

    static void Main(string[] args)
    {
        f5();
    }

当我不使用异步方法时,该程序可以完美运行,但是由于某种原因,处理程序没有被调用。Web 服务托管在不同域的不同计算机上,但客户端和服务器连接到同一网络。最重要的是,如果我改用 UploadString,一切正常。

谢谢

4

2 回答 2

2

与其尝试让 Async 方法工作,不如尝试使用非异步方法,但使用Task Parallel Library使其异步工作:

var client = new WebClient();
var data = File.ReadAllText("request.xml");
client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
client.Headers.Add("SOAPAction", "some string");

Task.Factory.StartNew(() =>
{
    string returnVal = client.UploadString(new Uri("http://differentdomain/wcf/Service.svc"), data);
    Console.WriteLine(returnVal);
});

这是一个整体上更好的策略,因为它适用于所有长时间运行的操作,而不仅仅是那些具有 Async 方法和事件处理程序的操作。

它还应该使发生的任何通信/传输错误更容易被发现。

于 2012-06-27T04:51:19.693 回答
1

您的程序在调用 UploadStringAsync 后立即退出,因此没有时间获得响应。Thread.Sleep在下面的代码中,如果我在 Main 方法的末尾删除调用,它也不会打印任何内容。在退出程序之前尝试等待响应到达。

public class StackOverflow_11218045
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }
    public static void Main()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        string data = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
                            <s:Header/>
                            <s:Body>
                                <Echo xmlns=""http://tempuri.org/"">
                                    <text>Hello</text>
                                </Echo>
                            </s:Body>
                        </s:Envelope>";
        var client = new WebClient();
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        client.Headers[HttpRequestHeader.ContentType] = "text/xml; charset=utf-8";
        client.Headers.Add("SOAPAction", "http://tempuri.org/ITest/Echo");

        ManualResetEvent evt = new ManualResetEvent(false);
        client.UploadStringAsync(new Uri(baseAddress), "POST", data, evt);
        evt.WaitOne();
    }

    static void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        Console.WriteLine(e.Result);
        ((ManualResetEvent)e.UserState).Set();
    }
}
于 2012-06-27T15:59:32.010 回答