我在互联网上托管了一个WCF服务。我可以使用Windows应用商店应用程序调用该服务,所以我知道它正在运行。我试图通过Windows Phone 8调用相同的服务。我已经创建了服务引用,并使用以下代码调用它:
using System.ServiceModel;
using System.Threading;
using System.Threading.Tasks;
using TP.Stamping.WCF.WP.Client;
using TP.Stamping.WCF.WP.Client.TPStampingClient;
namespace TP.Stamping.WCF.Client
{
public static class StampingServiceFacade
{
public static RegistrationResult RegisterUser(
string userName, string password,
string email, string phoneNumber,
string address1, string address2,
string city, string province,
string country, string postal)
{
return
RegisterUserAsync(userName, password, email, phoneNumber, address1, address2, city, province, country,
postal).Result;
}
public static async Task<RegistrationResult> RegisterUserAsync(
string userName, string password,
string email, string phoneNumber,
string address1, string address2,
string city, string province,
string country, string postal)
{
var endpointAddress =
new EndpointAddress(
"http://ourserver/TP.Stamping.WCF.Registration.StampingRegistrationService.svc");
var client = new StampingRegistrationServiceClient("HttpEndpoint", endpointAddress);
var waiter = new RegistrationWait(client);
client.RegisterNewUserAsync(userName, ComputeMD5(password), email, phoneNumber, address1, address2,
city, province, country, postal);
var results = await waiter.WaitTask();
return results;
}
private static string ComputeMD5(string str)
{
return MD5CryptoServiceProvider.GetMd5String(str);
}
private class RegistrationWait
{
private RegistrationResult _result;
public RegistrationWait(StampingRegistrationServiceClient client)
{
client.RegisterNewUserCompleted += ClientOnRegisterNewUserCompleted;
}
private void ClientOnRegisterNewUserCompleted(object sender, RegisterNewUserCompletedEventArgs registerNewUserCompletedEventArgs)
{
_result = registerNewUserCompletedEventArgs.Result;
}
public Task<RegistrationResult> WaitTask()
{
while(_result == RegistrationResult.Error) Thread.Sleep(50);
return new Task<RegistrationResult>(() => _result);
}
}
}
}
问题在于,当我从手机拨打服务时,它永远不会完成。在ClientOnRegisterNewUserCompleted
上设置断点永远不会被调用。我已经在这几个小时了,非常感谢帮助!