我正在通过快速入门来研究Prism v2。我已创建了以下签名的WCF服务:从服务引用生成的类中解耦Silverlight客户端
namespace HelloWorld.Silverlight.Web
{
[ServiceContract(Namespace = "http://helloworld.org/messaging")]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorldMessageService
{
private string message = "Hello from WCF";
[OperationContract]
public void UpdateMessage(string message)
{
this.message = message;
}
[OperationContract]
public string GetMessage()
{
return message;
}
}
}
当我在我的Silverlight项目添加服务引用该服务将生成一个接口和一个类:
[System.ServiceModel.ServiceContractAttribute
(Namespace="http://helloworld.org/messaging",
ConfigurationName="Web.Services.HelloWorldMessageService")]
public interface HelloWorldMessageService {
[System.ServiceModel.OperationContractAttribute
(AsyncPattern=true,
Action="http://helloworld.org/messaging/HelloWorldMessageService/UpdateMessage",
ReplyAction="http://helloworld.org/messaging/HelloWorldMessageService/UpdateMessageResponse")]
System.IAsyncResult BeginUpdateMessage(string message, System.AsyncCallback callback, object asyncState);
void EndUpdateMessage(System.IAsyncResult result);
[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://helloworld.org/messaging/HelloWorldMessageService/GetMessage", ReplyAction="http://helloworld.org/messaging/HelloWorldMessageService/GetMessageResponse")]
System.IAsyncResult BeginGetMessage(System.AsyncCallback callback, object asyncState);
string EndGetMessage(System.IAsyncResult result);
}
public partial class HelloWorldMessageServiceClient : System.ServiceModel.ClientBase<HelloWorld.Core.Web.Services.HelloWorldMessageService>, HelloWorld.Core.Web.Services.HelloWorldMessageService {
{
// implementation
}
我试图通过传递接口而不是具体类来解耦我的应用程序。但我很难找到如何做到这一点的例子。当我尝试调用EndGetMessage然后更新我的UI时,我得到了关于在错误线程上更新UI的异常。我如何从后台线程更新UI?
我试过但我得到UnauthorizedAccessException : Invalid cross-thread access
。
string messageresult = _service.EndGetMessage(result);
Application.Current.RootVisual.Dispatcher.BeginInvoke(() => this.Message = messageresult);
Application.Current.RootVisual
抛出异常。
我试过,但我得到UnauthorizedAccessException:无效的跨线程访问。 string messageresult = _service.EndGetMessage(result); Application.Current.RootVisual.Dispatcher.BeginInvoke(()=> this.Message = messageresult ); Application.Current.RootVisual抛出异常 – 2009-03-06 01:20:12