有没有人能够在 Windows Phone Series 7 模拟器上使用 WCF 进行通信?
过去两天我一直在尝试,这对我来说正在发生。我可以让普通的 Silverlight 控件在 Silverlight 3 和 Silverlight 4 中工作,但不能在手机版本中工作。这是我尝试过的两个版本:
版本 1 - 使用异步模式
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("http://localhost/wcf/Authentication.svc");
Wcf.IAuthentication auth1 = new ChannelFactory<Wcf.IAuthentication>(basicHttpBinding, endpointAddress).CreateChannel(endpointAddress);
AsyncCallback callback = (result) =>
{
Action<string> write = (str) =>
{
this.Dispatcher.BeginInvoke(delegate
{
//Display something
});
};
try
{
Wcf.IAuthentication auth = result.AsyncState as Wcf.IAuthentication;
Wcf.AuthenticationResponse response = auth.EndLogin(result);
write(response.Success.ToString());
}
catch (Exception ex)
{
write(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.Message);
}
};
auth1.BeginLogin("user0", "test0", callback, auth1);
这个版本打破了这一行:
Wcf.IAuthentication auth1 = new ChannelFactory<Wcf.IAuthentication>(basicHttpBinding, endpointAddress).CreateChannel(endpointAddress);
投掷System.NotSupportedException
。异常不是很具有描述性,调用堆栈同样不是很有帮助:
在 System.ServiceModel.DiagnosticUtility.ExceptionUtility.BuildMessage(异常 x) 在 System.ServiceModel.DiagnosticUtility.ExceptionUtility.LogException(异常 x) 在 System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(异常 e) 在 System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress 地址) 在 WindowsPhoneApplication2.MainPage.DoLogin() ……
版本 2 - 阻止 WCF 调用
这是不使用异步模式的版本。
[System.ServiceModel.ServiceContract]
public interface IAuthentication
{
[System.ServiceModel.OperationContract]
AuthenticationResponse Login(string user, string password);
}
public class WcfClientBase<TChannel> : System.ServiceModel.ClientBase<TChannel> where TChannel : class {
public WcfClientBase(string name, bool streaming)
: base(GetBinding(streaming), GetEndpoint(name)) {
ClientCredentials.UserName.UserName = WcfConfig.UserName;
ClientCredentials.UserName.Password = WcfConfig.Password;
}
public WcfClientBase(string name) : this(name, false) {}
private static System.ServiceModel.Channels.Binding GetBinding(bool streaming) {
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
binding.MaxReceivedMessageSize = 1073741824;
if(streaming) {
//binding.TransferMode = System.ServiceModel.TransferMode.Streamed;
}
/*if(XXXURLXXX.StartsWith("https")) {
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
}*/
return binding;
}
private static System.ServiceModel.EndpointAddress GetEndpoint(string name) {
return new System.ServiceModel.EndpointAddress(WcfConfig.Endpoint + name + ".svc");
}
protected override TChannel CreateChannel()
{
throw new System.NotImplementedException();
}
}
auth.Login("test0", "password0");
System.ServiceModel.ClientBase<TChannel>
此版本在构造函数中崩溃。调用堆栈有点不同:
在 System.Reflection.MethodInfo.get_ReturnParameter() 在 System.ServiceModel.Description.ServiceReflector.HasNoDisposableParameters(MethodInfo methodInfo) 在 System.ServiceModel.Description.TypeLoader.CreateOperationDescription(ContractDescription contractDescription,MethodInfo methodInfo,MessageDirection 方向,ContractReflectionInfo reflectInfo,ContractDescription declaringContract) 在 System.ServiceModel.Description.TypeLoader.CreateOperationDescriptions(ContractDescription contractDescription,ContractReflectionInfo 反射信息,类型 contractToGetMethodsFrom,ContractDescription declaringContract,MessageDirection 方向) 在 System.ServiceModel.Description.TypeLoader.CreateContractDescription(ServiceContractAttribute contractAttr,类型 contractType,类型 serviceType,ContractReflectionInfo&reflectionInfo,对象 serviceImplementation) 在 System.ServiceModel.Description.TypeLoader.LoadContractDescriptionHelper(类型 contractType,类型 serviceType,对象 serviceImplementation) 在 System.ServiceModel.Description.TypeLoader.LoadContractDescription(类型 contractType) 在 System.ServiceModel.ChannelFactory 1..ctor(绑定绑定,EndpointAddress 远程地址)1.CreateDescription() at System.ServiceModel.ChannelFactory.InitializeEndpoint(Binding binding, EndpointAddress address) at System.ServiceModel.ChannelFactory
在 System.ServiceModel.ClientBase 1..ctor(字符串名称,布尔流)1..ctor(Binding binding, EndpointAddress remoteAddress) at Wcf.WcfClientBase
在 Wcf.WcfClientBase`1..ctor(字符串名称) 在 Wcf.AuthenticationClient..ctor() 在 WindowsPhoneApplication2.MainPage.DoLogin() ...
有任何想法吗?